How can I determine the direction of a jQuery scroll event

0 votes

I'm looking for something to this effect:

$(window).scroll(function(event){
   if (/* magic code*/ ){
       // upscroll code
   } else {
      // downscroll code
   }
});

Any ideas?

Nov 24, 2020 in JQuery by kartik
• 37,520 points
868 views

1 answer to this question.

0 votes

Hello @kartik,

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;
});

Hope it helps!!

answered Nov 24, 2020 by Niroj
• 82,840 points

Related Questions In JQuery

0 votes
1 answer

How do I iterate through children elements of a div using jQuery?

Hello @kartik, Use children() and each(), you can optionally pass a ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,840 points
5,506 views
0 votes
1 answer

How can I convert a DOM element to a jQuery element?

Hello, Use this: var elm = document.createElement("div"); var jelm = ...READ MORE

answered Nov 25, 2020 in JQuery by Niroj
• 82,840 points
2,483 views
0 votes
1 answer

jQuery Event : Detect changes to the html/text of a div

Hello, If you don't want use timer and ...READ MORE

answered Nov 25, 2020 in JQuery by Niroj
• 82,840 points
20,536 views
0 votes
1 answer

How can I refresh a page with jQuery?

Use the JavaScript location. reload() Method You can simply ...READ MORE

answered Jun 10, 2022 in JQuery by gaurav
• 23,260 points
968 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,284 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,303 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
5,092 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7, 2020 in JQuery by Amit
• 140 points
2,923 views
0 votes
1 answer

How can you disable scroll bars in the jQuery UI dialog box?

Hello @kartik, I solved the problem like this: .dialog({ ...READ MORE

answered May 29, 2020 in JQuery by Niroj
• 82,840 points
5,596 views
0 votes
1 answer

How can I refresh a page with jQuery?

Hello @kartik, Use location.reload(): $('#something').click(function() { location.reload(); }); The reload() function ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,840 points
2,165 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP