Javascript Reference - Window scrollTo() Method








The scrollTo() method scrolls the content to the specified coordinates.

Browser Support

scrollTo Yes Yes Yes Yes Yes

Syntax

window.scrollTo(xpos, ypos)

Parameter Values

Parameter Description
xpos Required. The coordinate to scroll to, along the x-axis (horizontal)
ypos Required. The coordinate to scroll to, along the y-axis (vertical)




Return Value

No return value.

Example

The following code shows how to scroll the content to the horizontal position 500.


<!DOCTYPE html>
<html>
<body>
<button onclick="scrollWin()">test</button><br><br>
<div style="border:1px solid black;background-color:lightblue;height:75px;width:5000px;">
</div><!--from  w w w  .  j  a  v  a  2 s. co m-->

<script>
function scrollWin()
{
    window.scrollTo(500,0);
}
</script>

</body>
</html>

The code above is rendered as follows: