Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  53] [ 2]  / answers: 1 / hits: 36842  / 10 Years ago, sun, march 23, 2014, 12:00:00

I'm trying to think of a way to measure the velocity of a scroll event, that would produce some sort of a number which will represent the speed (distance from scroll point A to point B relative to the time it took).





I would welcome any suggestions in the form of pseudo code...
I was trying to find information on this problem, online but could not find anything. very weird since it's 2014, how could it be that there is nothing on google for this...weird!


More From » events

 Answers
6



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.clear()
console.log( checkScrollSpeed() );
};

body{ height:300vh }




Demo page:
http://codepen.io/vsync/pen/taAGd/


Simplified demo:
http://jsbin.com/mapafadako/edit?js,console,output




For real fun, give a real website these rules, then copy the JS and run it
[#71833] Friday, March 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chase

Total Points: 78
Total Questions: 106
Total Answers: 93

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
chase questions
Thu, Mar 31, 22, 00:00, 2 Years ago
Thu, Jul 1, 21, 00:00, 3 Years ago
Sat, Dec 12, 20, 00:00, 4 Years ago
Mon, Sep 14, 20, 00:00, 4 Years ago
;