Created
February 17, 2016 09:36
-
-
Save pocotan001/23cb2d15789a94825084 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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