Back to Content

Window: scroll() method

files/en-us/web/api/window/scroll/index.md

latest1.8 KB
Original Source

{{APIRef}}

The Window.scroll() method scrolls the window to a particular place in the document.

Syntax

js-nolint
scroll(xCoord, yCoord)
scroll(options)

Parameters

  • xCoord
    • : The pixel along the horizontal axis of the document that you want displayed in the upper left.
  • yCoord
    • : The pixel along the vertical axis of the document that you want displayed in the upper left.
  • options
    • : An object containing the following properties:
      • top
        • : Specifies the number of pixels along the Y axis to scroll the window or element.
      • left
        • : Specifies the number of pixels along the X axis to scroll the window or element.
      • behavior
        • : Determines whether scrolling is instant or animates smoothly. This option is a string which must take one of the following values:
          • smooth: scrolling should animate smoothly
          • instant: scrolling should happen instantly in a single jump
          • auto: scroll behavior is determined by the computed value of {{cssxref("scroll-behavior")}}

Return value

None ({{jsxref("undefined")}}).

Examples

js
// Put the 100th vertical pixel at the top of the window
window.scroll(0, 100);

Using options:

js
window.scroll({
  top: 100,
  left: 100,
  behavior: "smooth",
});

Notes

{{domxref("Window.scrollTo()")}} is effectively the same as this method. For relative scrolling, see {{domxref("Window.scrollBy()")}}, {{domxref("Window.scrollByLines()")}}, and {{domxref("Window.scrollByPages()")}}.

For scrolling elements, see {{domxref("Element.scrollTop")}} and {{domxref("Element.scrollLeft")}}.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • {{domxref("Window.scrollByLines()")}}
  • {{domxref("Window.scrollByPages()")}}
  • {{domxref("Element.scrollIntoView()")}}