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.
Return Value: This function does not return anything.
Example: In this example, we will implement the window.scrollTo() method on clicking a button.
html
< body style="width: 5000px; height: 5000px; margin-left: 260px;"> < h1 style = "color:green;" > w3wiki </ h1 > < h2 > Window scrollTo() Method </ h2 > < p > A computer science portal for Beginner. </ 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 > |
Output:
We have a complete list of HTML DOM methods, to check those please go through the DOM Complete Reference article.
Supported Browsers: The browser supported by the Window scrollTo() Method are listed below:
- Google Chrome 1 and above
- Edge 12 and above
- Internet Explorer 4 and above
- Firefox 1.0 and above
- Opera 4 and above
- Safari 1 and above
Contact Us