Skip to content

Instantly share code, notes, and snippets.

@HektorW
Last active August 29, 2015 14:21
Show Gist options
  • Save HektorW/d44f54d8d802a19547b3 to your computer and use it in GitHub Desktop.
Save HektorW/d44f54d8d802a19547b3 to your computer and use it in GitHub Desktop.
var current = 0;
var target = 0;
var updateDelay = 0;
var speed = 30 / 1000;
var lastTime = performance.now();
function step() {
requestAnimationFrame(step);
var time = performance.now();
var sinceLastUpdate = (time - lastTime) / 1000;
lastTime = time;
if (updateDelay > 0) {
updateDelay = Math.max(-speed, updateDelay - sinceLastUpdate);
}
if (current === target) return;
var direction = current < target ? +1 : -1;
while (current !== target && updateDelay <= 0) {
current += direction;
updateDelay += speed;
}
}
window.onscroll = function() {
target = window.scrollY; // ?
};
step();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment