监测scroll

    $(window).scroll(function() {
        var scrollValue = $(this).scrollTop(); 
        var h=200+scrollValue;
        $('.yui3-widget.yui3-overlay.yui3-widget-positioned').css('top', h);
    });

这里h 可以是 54  '54'  '54px' 等三种表达形式
scrollValue 本身就是数值  无需转换

 

最初我写成了这样

        var top=$('.yui3-widget.yui3-overlay.yui3-widget-positioned').css('top');
        top=parseInt(top.substr(0,top.length-2));

        var scrollValue = $(this).scrollTop(); 
        var h=top+scrollValue;
        h=parseInt(h);
        $('p.notice').text(h);
        $('.yui3-widget.yui3-overlay.yui3-widget-positioned').css('top', h);

这样是不能正确控制悬浮层的位置的    

因为我这里的h是有原本的top加上scrollTop的值   但是随着我的滚动   原本的top值就在不断的改变

那么div本身的top在之后设定后又成为的新的top值   所以我这样做  是top只不断的重复计算了

posted @ 2014-02-23 13:55  cart55free99  阅读(202)  评论(0编辑  收藏  举报