Created
June 21, 2015 16:18
-
-
Save benadamstyles/1d7e94bd2fbfa0e177fd to your computer and use it in GitHub Desktop.
Smooth Native Scroll
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
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