English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Window scrollTo() method

JavaScript Window Object

scrollTo()the method scrolls the document to the specified coordinate position.

The scrollTo() method scrolls the document to an absolute position, whilewindow.scrollBy()Scroll the document by the specified number of pixels.

For information on scrollable elements, seeelement.scrollTopandelement.scrollLeft.

Syntax:

window.scrollTo(x-coord, y-coord)
<button onclick="window.scrollTo(5">Click</button>
Test and See‹/›

Browser compatibility

All browsers fully support the scrollTo() method:

Method
scrollTo()IsIsIsIsIs

Parameter Value

ParameterDescription
x-coordThe number of pixels to display on the document horizontal axis at the top left
y-coordThe number of pixels to display on the document vertical axis, located at the top left corner

Technical Details

Return Value:None

More Examples

Vertical Document Scrolling500 pixels:

<button onclick="window.scrollTo(0, 500);">Click</button>
Test and See‹/›

Horizontal and Vertical Document Scrolling:

<button onclick="window.scrollTo(0, 50)">Scroll Down</button>
<button onclick="window.scrollTo(0, -50)">Scroll Up</button>
<button onclick="window.scrollTo(300, 0)">Scroll Right</button>
<button onclick="window.scrollTo(-300, 0)">Scroll Left</button>
Test and See‹/›

Related References

Window (Window) Reference:window.scrollBy() method

HTML DOM Reference:element .scrollLeft property

HTML DOM Reference:element .scrollTop property

JavaScript Window Object