Skip to content
Related Articles

Related Articles

Improve Article

Javascript | Window scrollTo() Method

  • Last Updated : 26 Jul, 2019

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:
scrollTo
After clicking the button:
scrollTo
Supported Browsers: The browser supported by Window scrollTo() Method are listed below:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :