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.