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
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
yCoordparameter. left-
The x-coordinate of the document that you want the viewport's left edge to scroll to. This is the same as the
xCoordparameter. 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 thescroll-behaviorCSS property on the document.
Return value
None (undefined).
Examples
window.scrollTo(0, 1000);
Using options:
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> |