Created
March 2, 2015 20:43
-
-
Save bloodyowl/4703ed7b4244c3e01d98 to your computer and use it in GitHub Desktop.
animation
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
const origin = window.pageYOffset | |
const destination = 3000 | |
animate({ | |
duration : 500, | |
tick(progress) { | |
window.scrollTo( | |
0, | |
progress * destination + | |
(1 - progress) * origin | |
) | |
} | |
}) | |
.then(() => console.log("done")) |
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
const duration = 300 | |
const tick = () => {} | |
function animate({duration, tick} = {duration, tick}) { | |
return new Promise((resolve) => { | |
const start = Date.now() | |
const onTick = () => { | |
const now = Date.now() | |
const progress = (now - start) / duration | |
if(progress >= 1) { | |
tick(1) | |
resolve() | |
return | |
} | |
tick(progress) | |
requestAnimationFrame(onTick) | |
} | |
requestAnimationFrame(onTick) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment