Skip to content

Instantly share code, notes, and snippets.

@Leolik
Last active August 27, 2018 19:03
Show Gist options
  • Save Leolik/ae253d02d4925ccbe023aeec7c75d82e to your computer and use it in GitHub Desktop.
Save Leolik/ae253d02d4925ccbe023aeec7c75d82e to your computer and use it in GitHub Desktop.
scrollTop js
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