-
Notifications
You must be signed in to change notification settings - Fork 22.6k
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
Editorial review: Add docs for scroll snap events #36057
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6108c4
Add docs for scroll snap events
chrisdavidmills 1136a3a
Add scroll snap events guide
chrisdavidmills 3255339
Fixes for estelle review comments
chrisdavidmills 76957e2
Fixes for estelle review comments and guide example improvements
chrisdavidmills bc3d5c0
Fixes for estelle review comments
chrisdavidmills c2c3dc8
More tweaks and additions
chrisdavidmills File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
files/en-us/web/api/document/scrollsnapchange_event/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
title: "Document: scrollsnapchange event" | ||
short-title: scrollsnapchange | ||
slug: Web/API/Document/scrollsnapchange_event | ||
page-type: web-api-event | ||
status: | ||
- experimental | ||
browser-compat: api.Document.scrollsnapchange_event | ||
--- | ||
|
||
{{APIRef}}{{SeeCompatTable}} | ||
|
||
The **`scrollsnapchange`** event of the {{domxref("Document")}} interface is fired on the document [scroll container](/en-US/docs/Glossary/Scroll_container) at the end of a scrolling operation when a new scroll snap target is selected. | ||
|
||
This event works in much the same way as the {{domxref("Element")}} interface's [`scrollsnapchange`](/en-US/docs/Web/API/Element/scrollsnapchange_event) event, except that the overall HTML document has to be set as the scroll snap container (i.e., {{cssxref("scroll-snap-type")}} is set on the {{htmlelement("html")}} element). | ||
|
||
## Syntax | ||
|
||
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. | ||
|
||
```js | ||
addEventListener("scrollsnapchange", (event) => {}); | ||
|
||
onscrollsnapchange = (event) => {}; | ||
``` | ||
|
||
## Event type | ||
|
||
A {{domxref("SnapEvent")}}, which inherits from the generic {{domxref("Event")}} type. | ||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
Let's say we have a {{htmlelement("main")}} element containing significant content that causes it to scroll: | ||
|
||
```html | ||
<main> | ||
<!-- Significant content --> | ||
</main> | ||
``` | ||
|
||
The `<main>` element can be turned into a scroll container using a combination of CSS properties, for example: | ||
|
||
```css | ||
main { | ||
width: 250px; | ||
height: 450px; | ||
overflow: scroll; | ||
} | ||
``` | ||
|
||
We can then implement scroll snapping behavior on the scrolling content by specifying the {{cssxref("scroll-snap-type")}} property on the {{htmlelement("html")}} element: | ||
|
||
```css | ||
html { | ||
scroll-snap-type: block mandatory; | ||
} | ||
``` | ||
|
||
The following JavaScript snippet would cause the `scrollsnapchange` event to fire on the HTML document when a child of the `<main>` element becomes a newly-selected snap target. In the handler function, we set a `selected` class on the child referenced by the {{domxref("SnapEvent.snapTargetBlock")}}, which could be used to style it to look like it has been selected (for example, with an animation) when the event fires. | ||
|
||
```js | ||
document.addEventListener("scrollsnapchange", (event) => { | ||
event.snapTargetBlock.classList.add("selected"); | ||
}); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Document/scrollsnapchanging_event", "scrollsnapchanging")}} event | ||
- {{DOMxRef("Document/scrollend_event", "scrollend")}} event | ||
- {{domxref("SnapEvent")}} | ||
- CSS {{cssxref("scroll-snap-type")}} property | ||
- [CSS scroll snap module](/en-US/docs/Web/CSS/CSS_scroll_snap) | ||
- [Using scroll snap events](/en-US/docs/Web/CSS/CSS_scroll_snap/Using_scroll_snap_events) | ||
- [Scroll Snap Events](https://developer.chrome.com/blog/scroll-snap-events) on developer.chrome.com (2024) |
94 changes: 94 additions & 0 deletions
94
files/en-us/web/api/document/scrollsnapchanging_event/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--- | ||
title: "Document: scrollsnapchanging event" | ||
short-title: scrollsnapchanging | ||
slug: Web/API/Document/scrollsnapchanging_event | ||
page-type: web-api-event | ||
status: | ||
- experimental | ||
browser-compat: api.Document.scrollsnapchanging_event | ||
--- | ||
|
||
{{APIRef}}{{SeeCompatTable}} | ||
|
||
The **`scrollsnapchanging`** event of the {{domxref("Document")}} interface is fired on the [scroll container](/en-US/docs/Glossary/Scroll_container) when the browser determines a new scroll snap target is pending, i.e. it will be selected when the current scroll gesture ends. | ||
|
||
This event works in much the same way as the {{domxref("Element")}} interface's [`scrollsnapchanging`](/en-US/docs/Web/API/Element/scrollsnapchanging_event) event, except that the overall HTML document has to be set as the scroll snap container (i.e., {{cssxref("scroll-snap-type")}} is set on the {{htmlelement("html")}} element). | ||
|
||
## Syntax | ||
|
||
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. | ||
|
||
```js | ||
addEventListener("scrollsnapchanging", (event) => {}); | ||
|
||
onscrollsnapchanging = (event) => {}; | ||
``` | ||
|
||
## Event type | ||
|
||
A {{domxref("SnapEvent")}}, which inherits from the generic {{domxref("Event")}} type. | ||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
Let's say we have a {{htmlelement("main")}} element containing significant content that causes it to scroll: | ||
|
||
```html | ||
<main> | ||
<!-- Significant content --> | ||
</main> | ||
``` | ||
|
||
The `<main>` element can be turned into a scroll container using a combination of CSS properties, for example: | ||
|
||
```css | ||
main { | ||
width: 250px; | ||
height: 450px; | ||
overflow: scroll; | ||
} | ||
``` | ||
|
||
We can then implement scroll snapping behavior on the scrolling content by specifying the {{cssxref("scroll-snap-type")}} property on the {{htmlelement("html")}} element: | ||
|
||
```css | ||
html { | ||
scroll-snap-type: block mandatory; | ||
} | ||
``` | ||
|
||
The following JavaScript snippet would cause the `scrollsnapchanging` event to fire on the HTML document when a child of the `<main>` element becomes a pending snap target. In the handler function, we set a `pending` class on the child referenced by the {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} property, which could be used to style it differently when the event fires. | ||
|
||
```js | ||
document.addEventListener("scrollsnapchanging", (event) => { | ||
// remove previously-set "pending" classes | ||
const pendingElems = document.querySelectorAll(".pending"); | ||
pendingElems.forEach((elem) => { | ||
elem.classList.remove("pending"); | ||
}); | ||
|
||
// Set current pending snap target class to "pending" | ||
event.snapTargetBlock.classList.add("pending"); | ||
}); | ||
``` | ||
|
||
At the start of the function, we select all elements that previously had the `pending` class applied and remove it, so that only the most recent pending snap target is styled. | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Document/scrollsnapchange_event", "scrollsnapchange")}} event | ||
- {{DOMxRef("Document/scrollend_event", "scrollend")}} event | ||
- {{domxref("SnapEvent")}} | ||
- CSS {{cssxref("scroll-snap-type")}} property | ||
- [CSS scroll snap module](/en-US/docs/Web/CSS/CSS_scroll_snap) | ||
- [Using scroll snap events](/en-US/docs/Web/CSS/CSS_scroll_snap/Using_scroll_snap_events) | ||
- [Scroll Snap Events](https://developer.chrome.com/blog/scroll-snap-events) on developer.chrome.com (2024) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
files/en-us/web/api/element/scrollsnapchange_event/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
title: "Element: scrollsnapchange event" | ||
short-title: scrollsnapchange | ||
slug: Web/API/Element/scrollsnapchange_event | ||
page-type: web-api-event | ||
status: | ||
- experimental | ||
browser-compat: api.Element.scrollsnapchange_event | ||
--- | ||
|
||
{{APIRef}}{{SeeCompatTable}} | ||
|
||
The **`scrollsnapchange`** event of the {{domxref("Element")}} interface is fired on the [scroll container](/en-US/docs/Glossary/Scroll_container) at the end of a scrolling operation when a new scroll snap target has been selected, just before the corresponding {{domxref("Element/scrollend_event", "scrollend")}} event fires. | ||
|
||
A scrolling operation ends when the user finishes scrolling within a scroll container — for example using a touch gesture or by dragging the mouse pointer on a scroll bar — and releases the gesture. | ||
|
||
## Syntax | ||
|
||
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. | ||
|
||
```js | ||
addEventListener("scrollsnapchange", (event) => {}); | ||
|
||
onscrollsnapchange = (event) => {}; | ||
``` | ||
|
||
## Event type | ||
|
||
A {{domxref("SnapEvent")}}, which inherits from the generic {{domxref("Event")}} type. | ||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
Let's say we have a {{htmlelement("main")}} element containing significant content that causes it to scroll: | ||
|
||
```html | ||
<main> | ||
<!-- Significant content --> | ||
</main> | ||
``` | ||
|
||
The `<main>` element can be turned into a scroll container that snaps to its children when scrolled using a combination of the CSS {{cssxref("scroll-snap-type")}} property and other properties. For example: | ||
|
||
```css | ||
main { | ||
width: 250px; | ||
height: 450px; | ||
overflow: scroll; | ||
scroll-snap-type: block mandatory; | ||
} | ||
``` | ||
|
||
The following JavaScript snippet would cause the `scrollsnapchange` event to fire on the `<main>` element when one of its children becomes a newly-selected snap target. In the handler function, we set a `selected` class on the child referenced by the {{domxref("SnapEvent.snapTargetBlock")}} property, which could be used to style it to look like it has been selected (for example, with an animation) when the event fires. | ||
|
||
```js | ||
const scrollingElem = document.querySelector("main"); | ||
|
||
scrollingElem.addEventListener("scrollsnapchange", (event) => { | ||
event.snapTargetBlock.classList.add("selected"); | ||
}); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} event | ||
- {{DOMxRef("Document/scrollend_event", "scrollend")}} event | ||
- {{domxref("SnapEvent")}} | ||
- CSS {{cssxref("scroll-snap-type")}} property | ||
- [CSS scroll snap module](/en-US/docs/Web/CSS/CSS_scroll_snap) | ||
- [Using scroll snap events](/en-US/docs/Web/CSS/CSS_scroll_snap/Using_scroll_snap_events) | ||
- [Scroll Snap Events](https://developer.chrome.com/blog/scroll-snap-events) on developer.chrome.com (2024) |
89 changes: 89 additions & 0 deletions
89
files/en-us/web/api/element/scrollsnapchanging_event/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
title: "Element: scrollsnapchanging event" | ||
short-title: scrollsnapchanging | ||
slug: Web/API/Element/scrollsnapchanging_event | ||
page-type: web-api-event | ||
status: | ||
- experimental | ||
browser-compat: api.Element.scrollsnapchanging_event | ||
--- | ||
|
||
{{APIRef}}{{SeeCompatTable}} | ||
|
||
The **`scrollsnapchanging`** event of the {{domxref("Element")}} interface is fired on the [scroll container](/en-US/docs/Glossary/Scroll_container) when the browser determines a new scroll snap target is pending, i.e. it will be selected when the current scroll gesture ends. | ||
|
||
Specifically, this event fires during a scrolling gesture, each time the user moves over potential new snap targets. For example, the user could scroll slowly by dragging their finger on a touch screen device, or hold down the mouse button on a scroll bar and move the mouse. `scrollsnapchanging` can therefore fire multiple times for each scrolling gesture. | ||
|
||
However, it does not fire on all potential snap targets for a scrolling gesture that moves over multiple snap targets. Rather, it fires just for the last target that the snapping will potentially rest on. | ||
|
||
## Syntax | ||
|
||
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property. | ||
|
||
```js | ||
addEventListener("scrollsnapchanging", (event) => {}); | ||
|
||
onscrollsnapchanging = (event) => {}; | ||
``` | ||
|
||
## Event type | ||
|
||
A {{domxref("SnapEvent")}}, which inherits from the generic {{domxref("Event")}} type. | ||
|
||
## Examples | ||
|
||
### Basic usage | ||
|
||
Let's say we have a {{htmlelement("main")}} element containing significant content that causes it to scroll: | ||
|
||
```html | ||
<main> | ||
<!-- Significant content --> | ||
</main> | ||
``` | ||
|
||
The `<main>` element can be turned into a scroll container that snaps to its children when scrolled using a combination of the CSS {{cssxref("scroll-snap-type")}} property and other properties. For example: | ||
|
||
```css | ||
main { | ||
width: 250px; | ||
height: 450px; | ||
overflow: scroll; | ||
scroll-snap-type: block mandatory; | ||
} | ||
``` | ||
|
||
The following JavaScript snippet would cause the `scrollsnapchanging` event to fire on the `<main>` element when one of its children becomes a pending snap target. In the handler function, we set a `pending` class on the child referenced by the {{domxref("SnapEvent.snapTargetBlock", "snapTargetBlock")}} property, which could be used to style it differently when the event fires. | ||
|
||
```js | ||
scrollingElem.addEventListener("scrollsnapchanging", (event) => { | ||
// remove previously-set "pending" classes | ||
const pendingElems = document.querySelectorAll(".pending"); | ||
pendingElems.forEach((elem) => { | ||
elem.classList.remove("pending"); | ||
}); | ||
|
||
// Set current pending snap target class to "pending" | ||
event.snapTargetBlock.classList.add("pending"); | ||
}); | ||
``` | ||
|
||
At the start of the function, we select all elements that previously had the `pending` class applied and remove it, so that only the most recent pending snap target is styled. | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}} event | ||
- {{DOMxRef("Document/scrollend_event", "scrollend")}} event | ||
- {{domxref("SnapEvent")}} | ||
- CSS {{cssxref("scroll-snap-type")}} property | ||
- [CSS scroll snap module](/en-US/docs/Web/CSS/CSS_scroll_snap) | ||
- [Using scroll snap events](/en-US/docs/Web/CSS/CSS_scroll_snap/Using_scroll_snap_events) | ||
- [Scroll Snap Events](https://developer.chrome.com/blog/scroll-snap-events) on developer.chrome.com (2024) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than add a really trivial example that is very similar to the standard event syntax section, I've elected to just include the syntax section on the document and window events pages.