Skip to content

Instantly share code, notes, and snippets.

@benadamstyles
Created June 21, 2015 16:18
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 benadamstyles/1d7e94bd2fbfa0e177fd to your computer and use it in GitHub Desktop.
Save benadamstyles/1d7e94bd2fbfa0e177fd to your computer and use it in GitHub Desktop.
Smooth Native Scroll
smoothScroll = speed => {
var endPos, comp, step;
const goScroll = function goScroll() {
scrollBy(0, step);
if (comp(window.scrollY, endPos)) {
window.requestAnimationFrame(goScroll);
}
},
runner = end => {
endPos = end;
if (end > window.scrollY) {
comp = _.lt;
step = speed;
window.requestAnimationFrame(goScroll);
} else {
comp = _.gt;
step = 0 - speed;
window.requestAnimationFrame(goScroll);
}
};
return {
by: by => runner(by + window.scrollY),
to: runner
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment