Title | Description | Subjects | Tags | CatalogContent | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
.scrollTo() |
Scrolls document to specified coordinate in pixels |
|
|
|
In JavaScript, .scrollTo()
scrolls the window or document to a specified position in pixels.
window.scrollTo(x, y)
x
: The horizontal coordinate (in pixels) to scroll to.y
: The vertical coordinate (in pixels) to scroll to.
Or, alternatively:
window.scrollTo(options)
options
: An object with the following optional properties:left
: The horizontal scroll position in pixels.top
: The vertical scroll position in pixels.behavior
: Defines the scrolling behavior. Accepted values:smooth
: Scrolls with an animation.instant
: Scrolls immediately.auto
: Uses the browser's default scrolling behavior.
The code below scrolls the window to 298 pixels from the left (x-axis) and 57 pixels from the top (y-axis) using absolute coordinates:
window.scrollTo(298, 57);
The code below scrolls the window smoothly to 57 pixels from the top (y-axis) and 298 pixels from the left (x-axis) using the options object:
window.scrollTo({
top: 57,
left: 298,
behavior: 'smooth',
});