So if you want to catch the moment the client is scrolling up or down, then for best practice you should use the 'wheel' event listner.
window.addEventListener("wheel", event => {
/* code here */
});
Now for capturing the actual moment the user is scrolling. use the 'deltaY' or 'wheelDeltaY' property's of the 'event' object to check its value and do something at a value below or above 0 like this.
window.addEventListener("wheel", ev => {
const direction_1 = ev.deltaY;
if (direction_1 < 0) console.log('scrolling up');
if (direction_1 > 0) console.log('scrolling down');
const direction_2 = ev.wheelDeltaY;
if (direction_2 > 0) console.log('scrolling up');
if (direction_2 < 0) console.log('scrolling down');
});
when scrolling up or down direction_1 or direction_2 always returns not an exact but nagative or positive value.
window.addEventListener("wheel", ev => {
const direction_1 = ev.deltaY;
if (direction_1 < 0) console.log(`deltaY | scroll up | ${direction_1}`);
if (direction_1 > 0) console.log(`deltaY | scroll down | ${direction_1}`);
const direction_2 = ev.wheelDeltaY;
if (direction_2 > 0) console.log(`wheelDeltaY | scroll up | ${direction_2}`);
if (direction_2 < 0) console.log(`wheelDeltaY | scroll down | ${direction_2}`);
});
react-lazyload
, where usingwindow.scrollTo
did. – Tuckerbag