How to scroll to a specific position in a browser window using JavaScript?

February 3, 2021 - 1 min read

To scroll to a specific position in the browser window, you can use the scrollTo() method in the global window object in JavaScript.

For example, to scroll 100px from the top of the page, you can pass 0 as the first argument (X Coordinate) and 100 as the second argument (Y Coordinate)to the scrollTo() method like this,

// Scroll 100px from top of the page 🌟
window.scrollTo(0, 100);

The scrollTo() method accepts 2 arguments:

  • The first argument is the X Coordinate to scroll.
  • The second argument is the Y Coordinate to scroll.

See this example live in JSBin.

Feel free to share if you found this useful 😃.