Window.scrollBy()

The Window.scrollBy() method scrolls the document in the window by the given amount.

Syntax

window.scrollBy(x-coord, y-coord);
window.scrollBy(options)

Parameters

  • x-coord is the horizontal pixel value that you want to scroll by.
  • y-coord is the vertical pixel value that you want to scroll by.

- or -

Examples

To scroll down one page:

window.scrollBy(0, window.innerHeight);

To scroll up:

window.scrollBy(0, -window.innerHeight);

Using options:

window.scrollBy({
  top: 100,
  left: 100,
  behavior: 'smooth'
});

Notes

window.scrollBy() scrolls by a particular amount, whereas window.scroll() scrolls to an absolute position in the document. See also window.scrollByLines() and window.scrollByPages().

Specifications

Specification
CSSOM View Module (CSSOM View)
# dom-window-scrollby

Browser compatibility

BCD tables only load in the browser