0

I am trying to run a function once the user has scrolled up 100px and another function when they scroll down (any number of pixels) from there current location. So if the user scrolls half way down a page and then starts to scroll up i need a function to run once they have scrolled up 100px from the current stored scrollTop value.

If the user was 700px down the page and they started to scroll up I need a function to run when they hit 600px. This should be dynamic from where ever the user is down the page.

As a starting point i have:

var lastScrollTop = 0;

$(window).scroll(function(event){
    var st = $(this).scrollTop();

    if (st > lastScrollTop){
        // Scrolling down
    } else {
        // Scrolling up
    }
    lastScrollTop = st;
});

Thanks

4

1 回答 1

0

jsFiddle(http://jsfiddle.net/nick_craver/gWD66/1/)

试试这个链接。可能对你很有帮助

使用本准则

 <script type="text/javascript">
var position = $(window).scrollTop();
$(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if(scroll > position) {
      moveCarDownward(scroll);  // $('#status').html('going down!');
    } else {
       moveCarUpward(scroll);  //$('#status').html('going up!');
    }
    position = scroll;
});

另一个链接 javascript链接中的哪一个

于 2013-04-18T11:54:04.640 回答