0

From this question on - How can I determine the direction of a jQuery scroll event?, I wrote this code.

var lastS=$(window).scrollTop();

function whatHappened()
{
    var currentS=$(window).scrollTop();
    console.log(lastS,currentS);

    if(currentS>lastS)
    {
       lastS = currentS;
       return "Scroll down";
    }
    else
    {
        lastS = currentS;
        return "Scroll up";
    }
}

$(window).on("mousewheel",function(){
    console.log(whatHappened());
});

This seems to work fine but there is a small problem. If I scroll down more than two times and then scroll up it first shows scroll down then scroll up. Similar happens in vice-versa. What's wrong? Please help. Thanks in advance.

JS Fiddle: http://jsfiddle.net/3843e/

Community
  • 1
  • 1
Rishi
  • 45
  • 9

1 Answers1

0

The mousewheel event is non-standard - in your case, it's probably getting triggered before scroll information is updated, so you're always operating on the "previous" scroll height.

Sacho
  • 2,169
  • 1
  • 14
  • 13