diff --git a/files/en-us/web/api/element/scroll/index.md b/files/en-us/web/api/element/scroll/index.md index d9378e4fe1f9043..103058522041cd2 100644 --- a/files/en-us/web/api/element/scroll/index.md +++ b/files/en-us/web/api/element/scroll/index.md @@ -8,9 +8,7 @@ browser-compat: api.Element.scroll {{APIRef("CSSOM view API")}} -The **`scroll()`** method of the {{domxref("Element")}} -interface scrolls the element to a particular set of coordinates inside a given -element. +The **`scroll()`** method of the {{domxref("Element")}} interface scrolls to a particular set of coordinates inside a given element. ## Syntax @@ -22,29 +20,34 @@ scroll(options) ### Parameters - `xCoord` - - : The pixel along the horizontal axis of the element that you want displayed in the - upper left. + - : The pixel along the horizontal axis of the element that you want displayed in the upper left. - `yCoord` - - : The pixel along the vertical axis of the element that you want displayed in the - upper left. + - : The pixel along the vertical axis of the element that you want displayed in the upper left. - `options` - : An object containing the following properties: - - `top` + - `top` {{optional_inline}} - : Specifies the number of pixels along the Y axis to scroll the window or element. - - `left` + - `left` {{optional_inline}} - : 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")}} + - `behavior` {{optional_inline}} + - : 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 {{cssxref("scroll-behavior")}} CSS property on the element. + + If omitted, `behavior` defaults to `auto`. ### Return value -None ({{jsxref("undefined")}}). +A {{jsxref("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 same element before the first one finishes. ## Examples +### Basic usage + ```js // Put the 1000th vertical pixel at the top of the element element.scroll(0, 1000); @@ -60,6 +63,129 @@ element.scroll({ }); ``` +### Responding to the end of the scroll + +Our [element methods demo](https://mdn.github.io/dom-examples/scroll-promises/element-methods/) ([see source code](https://github.com/mdn/dom-examples/tree/main/scroll-promises/element-methods)) 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`](#behavior) option to `smooth`, or by setting the scrolling element's {{cssxref("scroll-behavior")}} property to `smooth`). + +#### HTML + +Our HTML includes a {{htmlelement("section")}} element containing several paragraphs of content and a {{htmlelement("div")}} element toolbar containing {{htmlelement("button")}} elements that trigger various scrolling operations on the `
`. + +```html +
+ + + + +
+ +
...
+``` + +#### CSS + +We give the `
` element a fixed {{cssxref("height")}} and an {{cssxref("overflow-y")}} value of `scroll` so that it scrolls vertically, and set its {{cssxref("scroll-behavior")}} property to `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +section { + border: 1px solid black; + padding: 20px; + margin-top: 60px; + height: 500px; + overflow-y: scroll; + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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 ` + + + + + +
...
+``` + +#### CSS + +We give the `
` element a fixed {{cssxref("height")}} and an {{cssxref("overflow-y")}} value of `scroll` so that it scrolls vertically, and set its {{cssxref("scroll-behavior")}} property to `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +section { + border: 1px solid black; + padding: 20px; + margin-top: 60px; + height: 500px; + overflow-y: scroll; + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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 ` + + + + + +
+ ... + +

...

+
+``` + +#### CSS + +We give the `
` element a fixed {{cssxref("height")}} and an {{cssxref("overflow-y")}} value of `scroll` so that it scrolls vertically, and set its {{cssxref("scroll-behavior")}} property to `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +section { + border: 1px solid black; + padding: 20px; + margin-top: 60px; + height: 500px; + overflow-y: scroll; + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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 ` + + + + + +
...
+``` + +#### CSS + +We give the `
` element a fixed {{cssxref("height")}} and an {{cssxref("overflow-y")}} value of `scroll` so that it scrolls vertically, and set its {{cssxref("scroll-behavior")}} property to `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +section { + border: 1px solid black; + padding: 20px; + margin-top: 60px; + height: 500px; + overflow-y: scroll; + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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 ` + + + + +

...

+ +

...

+ +... +``` + +#### CSS + +We give the {{cssxref(":root")}} element a {{cssxref("scroll-behavior")}} property value of `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +:root { + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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. -{{domxref("Window.scrollTo()")}} is effectively the same as this method. For relative -scrolling, see {{domxref("Window.scrollBy()")}}, {{domxref("Window.scrollByLines()")}}, -and {{domxref("Window.scrollByPages()")}}. +#### JavaScript + +We start by grabbing references to the ` + + + + +

...

+ +

...

+ +... +``` + +#### CSS + +We give the {{cssxref(":root")}} element a {{cssxref("scroll-behavior")}} property value of `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +:root { + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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 ` + + + -{{domxref("Window.scroll()")}} 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")}}. +

...

+ +... +``` + +#### CSS + +We give the {{cssxref(":root")}} element a {{cssxref("scroll-behavior")}} property value of `smooth` so that any scroll operations are animated smoothly over time rather than instantly. + +```css +:root { + scroll-behavior: smooth; +} +``` + +We also create two class selectors; when a `fade-out` or `fade-in` class is applied to an element, an {{cssxref("animation")}} is applied so that it smoothly fades out or in, respectively. We also define {{cssxref("@keyframes")}} blocks to define the required {{cssxref("opacity")}} changes for those animations. + +```css +.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 `