jquery on scroll down 1

jquery on scroll down

Check current scrollTop vs previous scrollTop

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

Here is what the above code is Doing:
1. We’re creating a variable called lastScrollTop and setting it to 0.
2. We’re using jQuery’s scroll() function to detect when the user scrolls.
3. We’re using jQuery’s scrollTop() function to get the current scroll position.
4. We’re comparing the current scroll position to the last scroll position.
5. If the current scroll position is greater than the last scroll position, the user is scrolling down.
6. If the current scroll position is less than the last scroll position, the user is scrolling up.
7. We’re setting the last scroll position to the current scroll position.

Similar Posts