Categories
JavaScript Answers

How to scroll back to the top of scrollable div with JavaScript?

Spread the love

Sometimes, we want to scroll back to the top of scrollable div with JavaScript.

In this article, we’ll look at how to scroll back to the top of scrollable div with JavaScript.

How to scroll back to the top of scrollable div with JavaScript?

To scroll back to the top of scrollable div with JavaScript, we can set the scrollTop property of the scrollable div to 0.

For instance, we write

const myDiv = document.getElementById("containerDiv");
//...
myDiv.scrollTop = 0;

to get the div with getElementById.

Then we set the scrollTop property of myDiv to 0 to scroll to the top of the scrollable div.

Conclusion

To scroll back to the top of scrollable div with JavaScript, we can set the scrollTop property of the scrollable div to 0.

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 *