Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguishing between the user scrolling and programmatically scrolling using Javascript

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?

like image 700
Leo Jiang Avatar asked Jul 17 '15 01:07

Leo Jiang


People also ask

How do I know my user scroll?

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.

What is scroll in JavaScript?

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.

What is onscroll event?

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.

Is jQuery scrollable?

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.


2 Answers

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.

like image 55
developerbmw Avatar answered Sep 22 '22 03:09

developerbmw


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

like image 21
Aramil Rey Avatar answered Sep 19 '22 03:09

Aramil Rey