Skip to content

Instantly share code, notes, and snippets.

@arielsalminen
Last active April 18, 2018 09:32
Show Gist options
  • Save arielsalminen/6cd87add10414563acd9 to your computer and use it in GitHub Desktop.
Save arielsalminen/6cd87add10414563acd9 to your computer and use it in GitHub Desktop.
Scrolling
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