Skip to content

Instantly share code, notes, and snippets.

@vctrfrnndz
Last active November 8, 2019 07:51
Show Gist options
  • Save vctrfrnndz/cdbaee467673e91f9a06 to your computer and use it in GitHub Desktop.
Save vctrfrnndz/cdbaee467673e91f9a06 to your computer and use it in GitHub Desktop.
function scrollAnimated(to, duration) {
var start = $(window).scrollTop(),
change = to - start,
currentTime = 0,
increment = 20;
function easeInOut(currentTime, startValue, change, duration) {
currentTime /= duration / 2;
if (currentTime < 1) {
return change / 2 * currentTime * currentTime + startValue;
}
currentTime--;
return -change / 2 * (currentTime * (currentTime - 2) - 1) + startValue;
}
function animateScroll() {
currentTime += increment;
var val = easeInOut(currentTime, start, change, duration);
$(window).scrollTop(val);
if (currentTime < duration) {
setTimeout(animateScroll, increment);
}
}
animateScroll();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment