Skip to content

Instantly share code, notes, and snippets.

@kevinmartin
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmartin/61625cfdbdffc8acdddd to your computer and use it in GitHub Desktop.
Save kevinmartin/61625cfdbdffc8acdddd to your computer and use it in GitHub Desktop.
function smoothScroll(scrollTargetY, speed) {
var nextTick,
currentTime = 0,
scrollY = window.scrollY || document.documentElement.scrollTop,
time = Math.max(0.1, Math.min(Math.abs(scrollY - scrollTargetY) / speed, 0.8));
nextTick = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
function easeInOutQuint(pos) {
if ((pos /= 0.5) < 1) {
return 0.5 * Math.pow(pos, 5);
}
return 0.5 * (Math.pow((pos - 2), 5) + 2);
}
(function tick() {
var p, t;
currentTime += 1 / 60;
p = currentTime / time;
t = easeInOutQuint(p);
if (p < 1) {
nextTick(tick);
window.scrollTo(0, scrollY + ((scrollTargetY - scrollY) * t));
} else {
window.scrollTo(0, scrollTargetY);
}
})();
}
smoothScroll(0, 1500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment