Skip to content

Instantly share code, notes, and snippets.

@vemarav
Forked from joshbeckman/animatedScrollTo.js
Created August 15, 2018 14:58
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 vemarav/81442a075571ac3a35e898505c3df3ad to your computer and use it in GitHub Desktop.
Save vemarav/81442a075571ac3a35e898505c3df3ad to your computer and use it in GitHub Desktop.
ScrollTo animation using pure javascript and no jquery
document.getElementsByTagName('button')[0].onclick = function () {
scrollTo(document.body, 0, 1250);
}
function scrollTo(element, to, duration) {
var start = element.scrollTop,
change = to - start,
currentTime = 0,
increment = 20;
var animateScroll = function(){
currentTime += increment;
var val = Math.easeInOutQuad(currentTime, start, change, duration);
element.scrollTop = val;
if(currentTime < duration) {
setTimeout(animateScroll, increment);
}
};
animateScroll();
}
//t = current time
//b = start value
//c = change in value
//d = duration
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