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

Published February 3, 2021

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 😃.


Share on: Facebook Twitter