Skip to content

Instantly share code, notes, and snippets.

@thelebster
Forked from ricardozea/script.js
Created November 9, 2022 18:29
Show Gist options
  • Save thelebster/00812a4332004fe68a6f63820064fb04 to your computer and use it in GitHub Desktop.
Save thelebster/00812a4332004fe68a6f63820064fb04 to your computer and use it in GitHub Desktop.
Smooth scroll to top of page (Improved!)
window.scrollTo({top: 0, behavior: "smooth"});
<button onclick='window.scrollTo({top: 0, behavior: "smooth"});'>Back to Top ↑</button>

Smooth scroll to top of page (Improved!)

If you need a plain JavaScript function to add a smooth scrolling back to the top of the page, you can use this script:

window.scrollTo({top: 0, behavior: "smooth"});

How? 👉 Just add this script to a <button> element in the onclick: attribute, like this (and place it at the bottom of your page):

<button onclick='window.scrollTo({top: 0, behavior: "smooth"});'>Back to Top ↑</button>

CodePen demo here: (https://codepen.io/ricardozea/pen/NWpgyjL) by Ricardo Zea on CodePen.

License.

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