-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
156 lines (117 loc) · 3.54 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
---
title: "Element: scrollend event"
short-title: scrollend
slug: Web/API/Element/scrollend_event
page-type: web-api-event
browser-compat: api.Element.scrollend_event
---
{{APIRef}}
The **`scrollend`** event fires when element scrolling has completed.
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 a Document is complete, see the {{domxref("Document/scrollend_event", "scrollend")}} event of `Document`.
## 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")}}.
## Example
### Using `scrollend` with an event listener
The following example shows how to use the `scrollend` event to detect when the user has stopped scrolling:
```css hidden
#scroll-box {
height: 100px;
width: 100px;
float: left;
overflow: scroll;
outline: 4px dotted;
margin: 4px;
}
#scroll-box-title {
position: fixed;
top: 5px;
left: 5px;
transform: translateX(0);
}
#large-element {
height: 200px;
width: 200px;
}
#output {
text-align: center;
}
```
```html
<div id="scroll-box">
<p id="scroll-box-title">Scroll me!</p>
<p id="large-element"></p>
</div>
<p id="output">Waiting on scroll events...</p>
```
```js
const element = document.querySelector("div#scroll-box");
const output = document.querySelector("p#output");
element.addEventListener("scroll", (event) => {
output.textContent = "Scroll event fired, waiting for scrollend...";
});
element.addEventListener("scrollend", (event) => {
output.textContent = "Scrollend event fired!";
});
```
{{EmbedLiveSample("Using_scrollend_with_an_event_listener", "100%", 130)}}
### Using `onscrollend` event handler property
The following example shows how to use the `onscrollend` event handler property to detect when the user has stopped scrolling:
```css hidden
#scroll-box {
height: 100px;
width: 100px;
float: left;
overflow: scroll;
outline: 4px dotted;
margin: 4px;
}
#scroll-box-title {
position: fixed;
top: 5px;
left: 5px;
transform: translateX(0);
}
#large-element {
height: 200px;
width: 200px;
}
#output {
text-align: center;
}
```
```html
<div id="scroll-box">
<p id="scroll-box-title">Scroll me!</p>
<p id="large-element"></p>
</div>
<p id="output">Waiting on scroll events...</p>
```
```js
const element = document.querySelector("div#scroll-box");
const output = document.querySelector("p#output");
element.onscroll = (event) => {
output.textContent = "Element scroll event fired, waiting for scrollend...";
};
element.onscrollend = (event) => {
output.textContent = "Element scrollend event fired!";
};
```
{{EmbedLiveSample("Using_onscrollend_event_handler_property", "100%", 130)}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Element `scroll` event](/en-US/docs/Web/API/Element/scroll_event)
- [Document `scrollend` event](/en-US/docs/Web/API/Document/scrollend_event)
- [Document `scroll` event](/en-US/docs/Web/API/Document/scroll_event)