JavaScript scroll()

Anonymous contributor's avatar
Anonymous contributor
Published Feb 16, 2025
Contribute to Docs

scroll() is a method of the global window object that programmatically scrolls the document to a specified position.

Syntax

The scroll() method accepts either two numerical parameters (x and y coordinates) or a particular object with specific properties that control scrolling behavior:

window.scroll(x, y)

Or, alternatively:

window.scroll(options)
  • x: The horizontal scroll position in pixels.
  • y: The vertical scroll position in pixels.
  • options (Optional): An object specifying scroll behavior:
    • top: Vertical position in pixels.
    • left: Horizontal position in pixels.
    • behavior: Defines the scrolling behavior:
      • "auto" (Default): Instantly jumps to position.
      • "smooth": Animates the scroll movement.

Example 1

The following example instantly scrolls the page to the top-left (0, 0) position:

window.scroll(0, 0);

Example 2

The following example smoothly scrolls the page 500 pixels down while keeping the horizontal position unchanged:

window.scroll({
top: 500,
left: 0,
behavior: 'smooth',
});

All contributors

Contribute to Docs

Learn JavaScript on Codecademy