Element: 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 January 2020.
The scrollTo() method of the Element
interface scrolls to a particular set of coordinates inside a given element.
This method is an alias for Element.scroll().
Syntax
scrollTo(xCoord, yCoord)
scrollTo(options)
Parameters
xCoord-
The x-coordinate of the element's scrollable content that you want the left edge of the element's scrollport to scroll to.
yCoord-
The y-coordinate of the element's scrollable content that you want the top edge of the element's scrollport to scroll to.
options-
An object containing the following properties:
top-
The y-coordinate of the element's scrollable content that you want the top edge of the element's scrollport to scroll to. This is the same as the
yCoordparameter. left-
The x-coordinate of the element's scrollable content that you want the left edge of the element's scrollport 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 element.
Return value
None (undefined).
Examples
element.scrollTo(0, 1000);
Using options:
element.scrollTo({
top: 100,
left: 100,
behavior: "smooth",
});
Specifications
| Specification |
|---|
| CSSOM View Module> # dom-element-scrollto> |