Created
September 26, 2021 12:35
-
-
Save viclafouch/02d4be5bc2b7d4bdb39d32c0779985c5 to your computer and use it in GitHub Desktop.
JavaScript native API for the smooth scrolling.
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 scrollTop(nodeElement = window) { | |
try { | |
// The new API | |
nodeElement.scroll({ | |
top: 0, | |
left: 0, | |
behavior: 'smooth' | |
}) | |
} catch (error) { | |
// For olders browsers | |
nodeElement.scrollTo(0, 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it ?
Scroll to the top of the page :
Scroll to the top of an element :