jQuery插件floatIt,浮动div并居中在上方或者下方

 

网上找的太重量级了,写了个轻量级的。满足自己的需求就好,以后再加各种浮动,比如右下角之类的。
/*
 * jQuery float it plugin
 * Version 1.0 (8-July-2010)
 * @requires jQuery v1.4 or later
 *
 * Copyright (c) 2009-2010 Tony ZHOU
 * Dual licensed under the MIT and GPL licenses:
 */
 
jQuery.fn.floatIt = function (options) {
    //parameter like { location: "bottom" } or { location: "top" }
    // by extending the default settings, we don't modify the argument
    settings = jQuery.extend({ location: "bottom" }, options);

    var h = $(window).height();
    var w = $(window).width();

    var topLocation = 0;
    var leftLocation = w / 2 - $(this).width()/2;

    var currentId = $(this).attr("id");

    switch (settings["location"].toLowerCase()) {
        case ("bottom"):
            topLocation = h - $(this).width();
            $(window).scroll(function () {
                var topLocation = $(document).scrollTop() + $(window).height() - $("#" + currentId).height(); //closure
                $("#" + currentId).css({ position: "absolute", top: topLocation, left: leftLocation });
            });
            break;
        case ("top"):
            topLocation = 0;
            $(window).scroll(function () {
                var topLocation = $(document).scrollTop(); //closure
                $("#" + currentId).css({ position: "absolute", top: topLocation, left: leftLocation });
            });
            break;
        default:
            topLocation = h - $(this).width();
            break;
    }

    $(this).css({ position: "absolute", top: topLocation, left: leftLocation });
};

用起来的时候直接$("divname").floatIt(),默认是bottom.

效果就是这样

2010070910074154.jpg

要完整例子的话请移步到 http://www.iamtonyzhou.com/javascript/jquery-plugin-float-div-on-the-top-or-bottom-and-keep-it-in-the-center/

点击下图的链接下载

2010070910284563.jpg

转载于:https://www.cnblogs.com/beyondjay/archive/2010/07/09/1773908.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值