This snippet listens for scrolling events using addEventListener
, sets a delayed timeout function to run a few milliseconds after the event, but with each scroll event it clears that timeout function so it doesn’t run.
When scrolling has stopped, the delayed function doesn’t get cleared and runs.
// Setup isScrolling variable
var isScrolling;
// Listen for scroll events
window.addEventListener('scroll', function ( event ) {
// Clear our timeout throughout the scroll
window.clearTimeout( isScrolling );
// Set a timeout to run after scrolling ends
isScrolling = setTimeout(function() {
// Run the callback
console.log( 'Scrolling has stopped.' );
}, 66);
}, false);
Source
https://gomakethings.com/detecting-when-a-visitor-has-stopped-scrolling-with-vanilla-javascript/