Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Alexandre887 wants to merge2 commits intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromAlexandre887:master-16
Open
Changes fromall 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 deletion2-ui/1-document/10-size-and-scroll-window/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp