Skip to content

Instantly share code, notes, and snippets.

@pocotan001
Created February 17, 2016 09:36
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 pocotan001/23cb2d15789a94825084 to your computer and use it in GitHub Desktop.
Save pocotan001/23cb2d15789a94825084 to your computer and use it in GitHub Desktop.
/**
* http://easings.net/#easeOutCubic
*/
function easeOutCubic(t) {
const _t = t - 1;
return _t * _t * _t + 1;
}
/**
* @param {Element} context
* @param {number} end
*/
function smoothScroll(context = window, end = 0) {
const duration = 500;
const clock = Date.now();
const start = context === window ? context.pageYOffset : context.scrollTop;
function step() {
const elapsed = Date.now() - clock;
const position = elapsed < duration ? start + (end - start) * easeOutCubic(elapsed / duration) : end;
if (context === window) {
window.scroll(0, position);
} else {
context.scrollTop = position;
}
if (elapsed < duration) {
requestAnimationFrame(step);
}
}
step();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment