Open In App

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:

Return Value: This function does not return anything.



Example: In this example, we will implement the window.scrollTo() method on clicking a button.




<body style="width: 5000px;
            height: 5000px;
            margin-left: 260px;">
    <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>

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:

Article Tags :