Keira Nighly Keira Nighly - 4 months ago 21
Javascript Question

How can I know whether the scroll of the user is UP or DOWN?

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.


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.