-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
158 lines (120 loc) · 3.9 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Document: scrollend event"
short-title: scrollend
slug: Web/API/Document/scrollend_event
page-type: web-api-event
browser-compat: api.Document.scrollend_event
---
{{APIRef}}
The **`scrollend`** event fires when the document view has completed scrolling.
Scrolling is considered completed when the scroll position has no more pending updates and the user has completed their gesture.
Scroll position updates include smooth or instant mouse wheel scrolling, keyboard scrolling, scroll-snap events, or other APIs and gestures which cause the scroll position to update.
User gestures like touch panning or trackpad scrolling aren't complete until pointers or keys have released.
If the scroll position did not change, then no scrollend event fires.
For detecting when scrolling inside an element is complete, see {{domxref("Element/scrollend_event", "Element: scrollend event")}}.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("scrollend", (event) => {});
onscrollend = (event) => {};
```
## Event type
A generic {{domxref("Event")}}.
## Examples
### Using Document `scrollend` with an event listener
The following example shows how to use the `scrollend` event with an event listener to detect when the user has stopped scrolling the document.
In the example, there is content in the embedded iframe that is taller and wider than the iframe itself, so scrolling within the iframe in both directions is possible.
When the user stops scrolling, the `scrollend` event fires:
```css hidden
* {
margin: 10px;
}
.box-wrapper {
width: 900px;
border: 4px dotted;
}
.box {
height: 100px;
width: 100px;
display: block;
border: 4px dotted;
border-radius: 10px;
}
#output {
text-align: center;
font-size: 1.2em;
position: sticky;
bottom: 0;
}
```
```html
<div class="box-wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<p id="output">Waiting on scroll events...</p>
```
```js
const output = document.querySelector("p#output");
document.addEventListener("scroll", (event) => {
output.innerHTML = `Document scroll event fired!`;
});
document.addEventListener("scrollend", (event) => {
output.innerHTML = `Document scrollend event fired!`;
});
```
{{EmbedLiveSample("Using_document_scrollend_with_an_event_listener", "100%", 200)}}
### Using `onscrollend` event handler property
The following example shows how to use the `scrollend` event handler property to detect when the user has stopped scrolling the document.
In the example, there is content in the embedded iframe that is taller and wider than the iframe itself, so scrolling within the iframe in both directions is possible.
This builds on the first example, but uses `document.onscrollend` instead of an event listener:
```css hidden
* {
margin: 10px;
}
.box-wrapper {
width: 900px;
border: 4px dotted;
}
.box {
height: 100px;
width: 100px;
display: block;
border: 4px dotted;
border-radius: 10px;
}
#output {
text-align: center;
font-size: 1.2em;
position: sticky;
bottom: 0;
}
```
```html
<div class="box-wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<p id="output">Waiting on scroll events...</p>
```
```js
document.onscroll = (event) => {
output.innerHTML = "Document scroll event fired!";
};
document.onscrollend = (event) => {
output.innerHTML = "Document scrollend event fired!";
};
```
{{EmbedLiveSample("Using_scrollend_with_an_event_handler_property", "100%", 200)}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Document `scroll` event](/en-US/docs/Web/API/Document/scroll_event)
- [Element `scrollend` event](/en-US/docs/Web/API/Element/scrollend_event)
- [Element `scroll` event](/en-US/docs/Web/API/Element/scroll_event)