views:

65

answers:

1

Hi,

I want to determine the scroll of the users. I'm using jQuery.. And jquery have .scroll event.. But the .scroll event can't determine whether the user is scrolling the page downwards or upwards.

+4  A: 

You can start with a variable like this:

var position = $(window).scrollTop(); // should start at 0

And then have something that monitors whether the scrollTop is going up or down:

$(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if(scroll > position) {
         // scrolling downwards
    } else {
         // scrolling upwards
    }
    position = scroll;
});

Here is an example of it at work.

Paolo Bergantino
Hi Paolo,I tried your advice.. But there seems to be another problem..var position = $(window).scrollTop(); $(window).scroll(function() { var scroll = $(window).scrollTop(); if(scroll > position) { $('body').get(0).scrollTo = 1500; } else { $('body').get(0).scrollTo = 500 } position = scroll; })When I checked the code in Firebug. Document.body is null. Why is that? I'm guessing that it's not in the scope.
Keira Nighly
Where did you get the $('body').get(0).scrollTo stuff from?
Paolo Bergantino
Oh, I mistakenly put scrollto.. it should be scrollTop!
Keira Nighly
Weird results in IE when using mouse wheel
Josh Stodola