Skip to content

Instantly share code, notes, and snippets.

@Bachana123
Created March 9, 2020 17:08
Show Gist options
  • Save Bachana123/18436b53429e97fbde7cf18981e517d4 to your computer and use it in GitHub Desktop.
Save Bachana123/18436b53429e97fbde7cf18981e517d4 to your computer and use it in GitHub Desktop.
scroll speed
var checkScrollSpeed = (function(settings){
settings = settings || {};
var lastPos, newPos, timer, delta,
delay = settings.delay || 50; // in "ms" (higher means lower fidelity )
function clear() {
lastPos = null;
delta = 0;
}
clear();
return function(){
newPos = window.scrollY;
if ( lastPos != null ){ // && newPos < maxScroll
delta = newPos - lastPos;
}
lastPos = newPos;
clearTimeout(timer);
timer = setTimeout(clear, delay);
return delta;
};
})();
// listen to "scroll" event
window.onscroll = function(){
console.log( checkScrollSpeed() );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment