Window: scrollTo() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Window.scrollTo() scrolls to a particular set of coordinates in the document.

Syntax

js
scrollTo(xCoord, yCoord)
scrollTo(options)

Parameters

xCoord

The x-coordinate of the document that you want the viewport's left edge to scroll to.

yCoord

The y-coordinate of the document that you want the viewport's top edge to scroll to.

options

An object containing the following properties:

top

The y-coordinate of the document that you want the viewport's top edge to scroll to. This is the same as the yCoord parameter.

left

The x-coordinate of the document that you want the viewport's left edge to scroll to. This is the same as the xCoord parameter.

behavior

Determines whether the scrolling is instant or animates smoothly. This option is a string that must take one of the following values:

  • smooth: The scrolling animates smoothly.
  • instant: The scrolling happens instantly in a single jump.
  • auto: The scroll behavior is determined by the computed value of the scroll-behavior CSS property on the document.

Return value

None (undefined).

Examples

js
window.scrollTo(0, 1000);

Using options:

js
window.scrollTo({
  top: 100,
  left: 100,
  behavior: "smooth",
});

Notes

Window.scroll() is effectively the same as this method. For relative scrolling, see Window.scrollBy(), Window.scrollByLines(), and Window.scrollByPages().

For scrolling elements, see Element.scrollTop and Element.scrollLeft.

Specifications

Specification
CSSOM View Module
# dom-window-scrollto

Browser compatibility