Last active
August 27, 2018 19:03
-
-
Save Leolik/ae253d02d4925ccbe023aeec7c75d82e to your computer and use it in GitHub Desktop.
scrollTop js
This file contains hidden or 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
function scrollTo(to = 0, duration= 1000) { | |
const start = window.pageYOffset; | |
const change = to - start; | |
const increment = 20; | |
let currentTime = 0; | |
const animateScroll = (() => { | |
currentTime += increment; | |
const val = Math.easeInOutQuad(currentTime, start, change, duration); | |
window.scroll(0, val) | |
if (currentTime < duration) { | |
requestAnimationFrame(animateScroll); | |
} | |
}); | |
requestAnimationFrame(animateScroll); | |
}; | |
Math.easeInOutQuad = function (t, b, c, d) { | |
t /= d/2; | |
if (t < 1) return c/2*t*t + b; | |
t--; | |
return -c/2 * (t*(t-2) - 1) + b; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment