Last active
July 12, 2021 03:25
-
-
Save objectfoo/f5a49ef5aeeadb45586e150dd00d30ec to your computer and use it in GitHub Desktop.
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
| export default function smoothScrollTop(): Promise<null> { | |
| let lastScrollTop = Infinity; | |
| return new Promise((resolve) => { | |
| const smoothScrollTopImpl = () => { | |
| const top = document.documentElement.scrollTop || document.body.scrollTop; | |
| if (top > 0 && lastScrollTop > top) { | |
| lastScrollTop = top; | |
| window.requestAnimationFrame(smoothScrollTopImpl); | |
| window.scrollTo(0, top - (top / 8)); | |
| } else { | |
| lastScrollTop = Infinity; | |
| resolve(null); | |
| } | |
| }; | |
| setTimeout(() => smoothScrollTopImpl(), 10); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment