Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created March 2, 2015 20:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bloodyowl/4703ed7b4244c3e01d98 to your computer and use it in GitHub Desktop.
Save bloodyowl/4703ed7b4244c3e01d98 to your computer and use it in GitHub Desktop.
animation
const origin = window.pageYOffset
const destination = 3000
animate({
duration : 500,
tick(progress) {
window.scrollTo(
0,
progress * destination +
(1 - progress) * origin
)
}
})
.then(() => console.log("done"))
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