日期:2014-05-16  浏览次数:20453 次

js页面滚动浮动层智能定位(jQuery) 类似淘宝工具条
HTML代码:
<div class="float" id="float"> 我是个腼腆羞涩的浮动层<img src="http://web2.qq.com/style/face/6.gif" title="含羞">,你可以轻轻地<a href="http://www.zhangxinxu.com/php/regist.php">点击我</a>(去注册页)。</div>JS代码:
$.fn.smartFloat = function() {
    var position = function(element) {
        var top = element.position().top, pos = element.css("position");
        $(window).scroll(function() {
            var scrolls = $(this).scrollTop();
            if (scrolls > top) {
                if (window.XMLHttpRequest) {
                    element.css({
                        position: "fixed",
                        top: 0
                    });   
                } else {
                    element.css({
                        top: scrolls
                    });   
                }
            }else {
                element.css({
                    position: "absolute",
                    top: top
                });   
            }
        });
    };
    return $(this).each(function() {
        position($(this));                        
    });
};

//绑定
$("#float").smartFloat();  <style>.demo{height:1500px;}.float{width:60px; padding:10px; border:1px solid #ffecb0; background-color:#fffee0; -moz-box-shadow:1px 1px 2px rgba(0,0,0,.2); -webkit-box-shadow:1px 1px 2px rgba(0,0,0,.2); box-shadow:1px 1px 2px rgba(0,0,0,.2); position:absolute; right:10%; top:131px;}</style>