Last active
April 18, 2018 09:32
-
-
Save arielsalminen/6cd87add10414563acd9 to your computer and use it in GitHub Desktop.
Scrolling
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
function easeInOutCubic(t, b, c, d) { | |
if ((t /= d / 2) < 1) { | |
return c / 2 * t * t * t + b; | |
} | |
return c / 2 * ((t -= 2) * t * t + 2) + b; | |
} | |
function ButteryScroll(scrollable, distance, duration) { | |
var startTime; | |
var startPos = scrollable.scrollTop; | |
var maxScroll = scrollable.scrollHeight - scrollable.offsetHeight; | |
var scrollEndValue = startPos + distance < maxScroll ? distance : maxScroll - startPos; | |
function scroll(timestamp) { | |
startTime = startTime || timestamp; | |
var elapsed = timestamp - startTime; | |
var progress = easeInOutCubic(elapsed, startPos, scrollEndValue, duration); | |
scrollable.scrollTop = progress; | |
if (elapsed < duration) { | |
requestAnimationFrame(scroll); | |
} | |
} | |
requestAnimationFrame(scroll); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment