Categories
JavaScript Answers

How to scroll to top of page with JavaScript?

Spread the love

To scroll to top of page with JavaScript, we set the scrollTop property of the body element to 0.

For instance, we write

document.body.scrollTop = document.documentElement.scrollTop = 0;

to set document.body.scrollTop and document.documentElement.scrollTop to 0 to scroll to the top of the page.

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 *