Skip to content
Related Articles

Related Articles

Javascript | Window scrollTo() Method

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 01 Jun, 2022

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: 

html




<!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: scrollTo After clicking the button: scrollTo Supported Browsers: The browser supported by 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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!