Baruch B. Baruch B. - 3 months ago 9
Javascript Question

How to detect all kind of scrolling without libraries?

I want to know how can I detect a scroll event on page Here are two different ways to scroll.

on desktop:


  1. By rolling the mouse wheel (the event handler is DOMElement.onwheel event handler).

  2. By pressing the mouse wheel and moving the cursor in appropriate direction (known also as autoscroll).



on mobile:


  1. by swapping the finger down or up



How can I detect the scroll event as described in the second and third way?

Answer

In all cases it is a scroll event, so:

window.addEventListener("scroll", function(e) {
  ...
});

Second and third events have one thing in common - they occur inside mousedown and mouseup frame. You can use that for event flavor detection.