Last active
November 8, 2019 07:51
-
-
Save vctrfrnndz/cdbaee467673e91f9a06 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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