scroll to top in javascript

To scroll to the top of a page using JavaScript, you can use the window.scroll() method.

index.tsx
// Scroll to top function
function scrollToTop() {
  window.scrollTo(0, 0);
}

// Call the function to scroll to top
scrollToTop();
132 chars
8 lines

The window.scrollTo() method takes two arguments: the x-coordinate and y-coordinate to which you want to scroll. In this case, we want to scroll to the top of the page, so we pass 0 for both the x-coordinate and y-coordinate.

You can call the scrollToTop() function whenever you need to scroll to the top of the page. For example, you could call it when the user clicks a button or scrolls to a certain point on the page.

Note that there are other ways to scroll to the top of a page using JavaScript. This is just one of them.

gistlibby LogSnag