检测水平条从一开始移动了多少

时间:2013-05-14 10:36:54

标签: jquery

我有一个固定的div,我想上下移动但不是左右移动,我设法在另一个答案的帮助下这样做。

问题是,如果我滚动到例如右边页面的末尾,然后我向下滚动,我得到固定div那里,但我只想显示水平条是否在开头(或向右移动一点)

是否可以检测水平条是否已从begnning移动了一定数量的像素?因为我想在horiz之后动态更改一些CSS。 bar已被移动,例如向右移动100px。

CSS和HTML:

#test {

    width: 100px;
    height: 100px;
    background: red;
    position: fixed;
    left: 0;
    top: 100px;
}

<div id="test">&nbsp;</div>

JQuery的:

$(function () {

    var lastScrollLeft = 0;
    jQuery(window).scroll(function() {
        var documentScrollLeft = jQuery(document).scrollLeft();

        if (lastScrollLeft != documentScrollLeft) {
            jQuery('#test').attr('style', 'position:absolute;left:0px;')
            lastScrollLeft = documentScrollLeft;
        }
    });


    var lastScrollTop = 0;

    jQuery(window).scroll(function() {
        var documentScrollTop = jQuery(document).scrollTop();
        if (lastScrollTop != documentScrollTop) {
            jQuery('#test').attr('style', 'position:fixed;left:0px;')
            lastScrollTop = documentScrollTop;
        }
    });
}); // end ready

总结一下:

在第二个滚动功能中,如果页面向右滚动100px,我想将div位置更改为绝对位置。

0 个答案:

没有答案