JavaScript Program to Scroll Webpage Using scrollTo() method

Write a JavaScript program to scroll webpage using "scrollto()" method.



Source Code
<html>
<head>
    <title>scrollTo() method</title>
    <script>
        function init()
        {
            scrollTo(80, 150)
        }
    </script>
</head>
<body onload="init()">
<h1>Javascript | Window scrollTo() Method</h1>
<p>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.
</p>
<pre>
Example:

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