Window: scroll() method The scroll() method of the Window interface scrolls the window to a particular place in the document. Syntax 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 that must take one of the following values: smooth: The scrolling animates smoothly. instant: The scrolling happens instantly in a single jump. auto: The scroll behavior is determined by the computed value of the scroll-behavior CSS property on the element. If omitted, behavior defaults to auto. Return value A Promise that fulfills with an object containing the following property: interrupted : A boolean value indicating whether the scrolling operation was interrupted ( true) or not ( false). Such an interruption typically happens when a programmatic scroll is ongoing, and another programmatic scroll is initiated on the window before the first one finishes. Examples Basic usage // Put the 100th vertical pixel at the top of the window window.scroll(0, 100); Using options: window.scroll({ top: 100, left: 100, behavior: "smooth", }); Responding to the end of the scroll Our window methods demo (see source code) demonstrates how the promise return value of scroll() can be used to respond to the end of a scrolling operation. This technique is mostly useful in cases where the scrolling occurs smoothly over time (achieved by setting the behavior option to smooth, or by setting the scrolling element's scroll-behavior property to smooth). HTML Our HTML includes several paragraphs of content and a div element toolbar containing button elements that trigger various scrolling operations on the window.
...
...
... CSS We give the :root element a scroll-behavior property value of smooth so that any scroll operations are animated smoothly over time rather than instantly. :root { scroll-behavior: smooth; } We also create two class selectors; when a fade-out or fade-in class is applied to an element, an animation is applied so that it smoothly fades out or in, respectively. We also define @keyframes blocks to define the required opacity changes for those animations. .fade-out { animation: fade-out 0.3s linear both; } .fade-in { animation: fade-in 0.3s linear both; } @keyframes fade-out { from { opacity: 1; } to { opacity: 0; } } @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } } The rest of the CSS is not shown, for brevity. JavaScript We start by grabbing references to the