Javascript | Window scrollTo() Method

The 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:
Before clicking the button:
scrollTo
After clicking the button:
scrollTo
Supported Browsers: The browser supported by disabled attribute for <optgroup> element are listed below:

  • Apple Safari
  • Google Chrome
  • Firefox 1.0
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.