Last active
January 17, 2019 02:22
-
-
Save ebidel/650c08970baca136ffd0ef67eba3767a to your computer and use it in GitHub Desktop.
Smooth scroll page
This file contains hidden or 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() { | |
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