We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Summary

Scrolls the window to a particular place in the document.

Syntax

window.scroll(x-coord, y-coord)
window.scroll(options) 

Parameters

  • x-coord is the pixel along the horizontal axis of the document that you want displayed in the upper left.
  • y-coord is the pixel along the vertical axis of the document that you want displayed in the upper left.

- or -

  • options is an object with three possible properties:
    • top, which is the same as the y-coord
    • left, which is the same as the x-coord
    • behavior, which is a string containing one of smooth, instant, or auto; default is auto

Example

<!-- put the 100th vertical pixel at the top of the window -->

<button onClick="scroll(0, 100);">click to scroll down 100 pixels</button>

Change the scrolling behavior

If you are looking to change how the scrolling works, you can specify that in the options object:

window.scroll({
  top: 1000,
  behavior: "smooth"
});

Notes

Window.scrollTo() is effectively the same as this method. For scrolling a particular distance, use Window.scrollBy().

Also see Window.scrollByLines(), Window.scrollByPages() and Element.scrollIntoView().

Specification

Specification Status Comment
CSS Object Model (CSSOM) View Module
The definition of 'window.scroll()' in that specification.
Working Draft

Initial definition.

Document Tags and Contributors

Last updated by: cwadrupldijjit,