JavaScript scroll()
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
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours