Skip to content

Added information about smooth scrolling (options object) #3558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion 2-ui/1-document/10-size-and-scroll-window/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,34 @@ Alternatively, there's a simpler, universal solution: special methods [window.sc
<button onclick="window.scrollTo(0,0)">window.scrollTo(0,0)</button>
```

These methods work for all browsers the same way.
Both methods can also use an `options` object as an argument instead of coordinates:

```js
window.scrollTo(options);
window.scrollBy(options);
```

`options` supports three properties:

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

- `top` -- same as `y`/`pageY`
- `left` -- same as `x`/`pageX`
- `behavior` -- determines how the page will scroll:
- `"smooth"` -- smoothly (not supported in IE and older versions of Safari)
- `"instant"` -- instantly
- `"auto"` -- the browser itself chooses (depends on the CSS property [scroll-behavior](https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior))

```online
Demonstration of smooth page scrolling:
<button onclick="window.scrollBy({ top: 500, left: 0, behavior: 'smooth' })">window.scrollBy({ top: 500, left: 0, behavior: 'smooth' })</button>
```

## scrollIntoView

Expand All @@ -129,6 +156,26 @@ And this button scrolls the page to position itself at the bottom:
<button onclick="this.scrollIntoView(false)">this.scrollIntoView(false)</button>
```

Like `scrollTo`/`scrollBy`, `scrollIntoView` also takes an `options` object as an argument (it is slightly different):

```js
this.scrollIntoView(options).
```

`options` supports three properties:

```js
this.scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest"
});
```

- `behavior` -- scroll animation (`smooth`, `instant`, `auto`)
- `block` -- vertical alignment (`start`, `center`, `end`, `nearest`). Defaults to `start`
- `inline` -- horizontal alignment (`start`, `center`, `end`, `nearest`). Defaults to `nearest`

## Forbid the scrolling

Sometimes we need to make the document "unscrollable". For instance, when we need to cover the page with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.
Expand Down