Last active
August 29, 2015 14:21
-
-
Save HektorW/d44f54d8d802a19547b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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