Skip to content

Instantly share code, notes, and snippets.

@viclafouch
Created September 26, 2021 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viclafouch/02d4be5bc2b7d4bdb39d32c0779985c5 to your computer and use it in GitHub Desktop.
Save viclafouch/02d4be5bc2b7d4bdb39d32c0779985c5 to your computer and use it in GitHub Desktop.
JavaScript native API for the smooth scrolling.
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)
}
}
@viclafouch
Copy link
Author

How to use it ?

Scroll to the top of the page :

scrollTop()

Scroll to the top of an element :

scrollTop(document.getElementById('my-app'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment