Javascript | Window scrollTo() Method
The Window scrollTo() method is used to scroll to a particular set of coordinates in the document.
Syntax:
window.scrollTo(x-coord, y-coord)
Parameters: The scrollTo() method accepts two parameters as described below:
- x-coord: It is the pixel along the horizontal axis of the document that is displayed in the upper left. It is the required field.
- y-coord: It is the pixel along the vertical axis of the document that is displayed in the upper left. It is the required field.
Example:
<!DOCTYPE html><html> <head> <title>Window scrollTo() Method</title> <style> body { width: 5000px; height: 5000px; margin-left: 260px; } </style></head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <h2> Window scrollTo() Method </h2> <p> A computer science portal for geeks. </p> <button onclick="geek()">Click to scroll!</button> <script> function geek() { //Scrolling the document to position "250" //horizontally and "110" vertically window.scrollTo(250, 110); } </script></body> </html> |
Output:
Before clicking the button:
After clicking the button:
Supported Browsers: The browser supported by Window scrollTo() Method are listed below:
- Google Chrome
- Internet Explorer
- Firefox 1.0
- Opera
- Safari



