I'm creating a scrolling effect using JQuery and I'm wondering if it's possible to distinguish between the user scrolling vs. programmatically scrolling.
I have something like this:
$('#element').on('scroll',function(e){ $('#element').stop(true); // stop previous scrolling animation $('#element').animate({ // start new scrolling animation (maybe different speed, different direction, etc) scrollTop:... }); });
However, this event is triggered during every step of the animation. How can I tell if this event was triggered by the user or by the animation?
To detect if a user is scrolling with JavaScript, we can watch for scroll events by adding event handlers. to add the userHasScrolled variable. Then we set the window. onscroll property to a function that runs when we scroll.
Definition and UsageThe scroll event occurs when the user scrolls in the specified element. The scroll event works for all scrollable elements and the window object (browser window). The scroll() method triggers the scroll event, or attaches a function to run when a scroll event occurs.
The onscroll event occurs when an element's scrollbar is being scrolled. Tip: use the CSS overflow style property to create a scrollbar for an element.
jQuery. scrollable manages animated scrolling in windows, scrollable elements and iframes. It frees you from handling gotchas and edge cases and offers convenient, flexible options for animation. If you are a happy user of this project already, you can support its development by donating to it.
Use a variable to determine when you are scrolling programmatically
Example:
var programScrolling = false; $('#element').on('scroll',function(e){ if (programScrolling) { return; } $('#element').stop(true); // stop scrolling animation programScrolling = true; $('#element').animate({ scrollTop:... }); programScrolling = false; });
Not sure if that is exactly what you want, but the concept should work.
I would make functions for different kinds of scrollings to detect them and call a scroll handler for all of them, like so:
JS Fiddle
$(window).bind('mousewheel DOMMouseScroll', function(event){ var direction; if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) { direction = 'up'; } else { direction = 'down'; } scrollHandler(direction, 'mouseWheel'); event.preventDefault(); }); var scrollHandler = function(direction, origin) { var height = $(document).scrollTop(); var movement = (direction == 'up') ? -100 : 100; console.log(origin); $('body').stop(true); $('body').animate({ scrollTop: height + movement }, 250); };
Then you can do different stuff according to the origin of the event!
You could also check if the user scrolls to the same direction that the screen is scrolling and do something different, or whatever you want with the info passed by the mousewheel event.
Original mousewheel event function copied from THIS answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With