Capture actions: Scroll Down and Scroll Up

Asked

Viewed 2,122 times

5

Is there any way in jQuery to capture the events of "Scrolldown" and "Scrollup"?

I tried this way:

var lastScrollTop = 0;
$(window).scroll(function(event){
   var st = $(this).scrollTop();
   if (st > lastScrollTop){
      // downscroll code
   } else {
      // upscroll code
   }
   lastScrollTop = st;
}); 

But it does not capture the function of UP and DOWN but the scroll any!

And I wanted that as soon as the mouse bearing was triggered the function was executed.

That’s possible ?

  • Samir, by the way, this answer was what I was looking for?

1 answer

5

The code you put in does what you want. There is no other property of the event that is direction indicator, you really have to measure.

If you want to know specifically how to measure the direction of the mouse wheel, then it is possible to take the properties of the evento (take a look here: How to know the scroll direction of the mouse wheel).

But the scroll is more comprehensive is triggered in other ways and not only the mouse wheel.

Take a look here to see your working code: jsfiddle.net/ayxqg/1/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.