You can use the window.scrollTo() method in JavaScript to scroll to the top of the page. Here's an example:
javascript
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
This code will smoothly scroll the page to the top-left corner. Adjust the behavior parameter to 'auto' for an instant scroll or 'smooth' for a smooth animated scroll.
To scroll to the top of the page using JavaScript, you can use the window.scrollTo() method or window.scroll() method. Here's an example:
javascript
// Using window.scrollTo()
window.scrollTo({ top: 0, behavior: 'smooth' }); // Scrolls smoothly to the top of the page
// Using window.scroll()
window.scroll({ top: 0, behavior: 'smooth' }); // Scrolls smoothly to the top of the page
Both methods will smoothly scroll the page to the top. Adjust the behavior parameter to 'smooth' for smooth scrolling or 'auto' for instant scrolling without animation.
scrollTop
property of the document.documentElement
or document.body
elements to 0
.