Skip to content

Instantly share code, notes, and snippets.

@ebidel
Last active January 17, 2019 02:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebidel/650c08970baca136ffd0ef67eba3767a to your computer and use it in GitHub Desktop.
Save ebidel/650c08970baca136ffd0ef67eba3767a to your computer and use it in GitHub Desktop.
Smooth scroll page
(function() {
const scroller = document.scrollingElement || document.body;
// Smooth scrolls to the bottom of the page.
window.smoothScrollPage = (y = scroller.scrollHeight) => {
scroller.style.scrollBehavior = 'smooth';
scroller.scrollTop = y;
scroller.style.scrollBehavior = '';
};
// Smooth scrolls to the bottom of the page at a rate of 60fps.
window.scrollPageSlowly = () => {
requestAnimationFrame(function callback(timestamp) {
scroller.scrollBy({left: 0, top: 100, behavior: 'smooth'});
const atBottom = scroller.getBoundingClientRect().bottom - window.innerHeight === 0;
if (!atBottom) {
requestAnimationFrame(callback);
}
});
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment