Categories
JavaScript Answers

How to smooth scroll to top with JavaScript?

Spread the love

Sometimes, we want to smooth scroll to top with JavaScript.

In this article, we’ll look at how to smooth scroll to top with JavaScript.

How to smooth scroll to top with JavaScript?

To smooth scroll to top with JavaScript, we call the window.scrollTo method.

For instance, we write

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

to call window.scrollTo with an object that specifies that we scroll to top position 0 and the scroll behavior is smooth to scroll to the top of the page with smooth scrolling.

Conclusion

To smooth scroll to top with JavaScript, we call the window.scrollTo method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *