Javascript Browser Window scrollTo() Method

Introduction

Scroll the document to the horizontal position "500":

window.scrollTo(500, 0);

Click the button to scroll the document window to 500 pixels horizontally.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
body {/*from  ww  w . ja  va2s  .  c o  m*/
  width: 5000px;
}
</style>
</head>
<body>

<button onclick="scrollWin()">Click me to scroll horizontally!</button><br><br>

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

</body>
</html>

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

Use the scrollBy() method to scroll a specified distance multiple times.

scrollTo(xpos,ypos);

Parameter Values

Parameter
Type
Description
xpos

Number Required.
The coordinate to scroll to, along the x-axis (horizontal), in pixels
ypos

Number Required.
The coordinate to scroll to, along the y-axis (vertical), in pixels



PreviousNext

Related