-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
291 lines (248 loc) · 6.71 KB
/
index.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { getEl } from './src/getElement'
import { eventsSetup, eventsInit, eventsDestroy } from './src/events'
export default class DetectScroll {
constructor(
el,
{
horizontal = true,
vertical = true,
debugMode = false,
events = undefined,
} = {}
) {
this.el = getEl(el)
this.x = this.getX()
this.y = this.getY()
this.lastDispatch = null
this.hasScrolled = false
this.isWindow = window === el
this.isScrolling = undefined
this.isVertical = vertical
this.isHorizontal = horizontal
this.rafId = null
this.rafTick = 0
this.rafKilled = false
this.timeout = null
this.destroy = this.destroy.bind(this)
this.onScroll = this.onScroll.bind(this)
this.events = eventsSetup(events, this.isVertical, this.isHorizontal)
this.debugMode = debugMode
this.hasInit = 0
this.destroyed = null
this.passiveMode = true
this.scrollingU = 0
this.scrollingD = 0
this.scrollingL = 0
this.scrollingR = 0
this.init()
}
init() {
// fetch x&y
if (this.isHorizontal) this.getX()
if (this.isVertical) this.getY()
// defines custom events to dispatch
eventsInit(this.el, this.events)
// main scroll event that informs everything
this.el.addEventListener('scroll', this.onScroll, {
passive: this.passiveMode,
})
// show dispatched events
if (this.debugMode && process.env.NODE_ENV === 'development') {
console.group('Detect Scroll Debugger')
console.log('Element', this.el)
console.log('Events', this.events)
console.groupEnd()
}
// watch for changes
this.watchScrollPosition()
// reset value if destroyed
this.destroyed = 0
this.hasInit = 1
}
destroy() {
// remove scroll event + timeout + custom events
this.el.removeEventListener('scroll', this.onScroll, {
passive: this.passiveMode,
})
// remove custom events
eventsDestroy(this.el, this.events)
// remove timeout (in case teardown fires mid-scroll)
window.clearTimeout(this.timeout)
// remove raf
window.cancelAnimationFrame(this.rafId)
this.rafKilled = true
this.events = undefined
this.destroyed = 1
this.hasInit = 0
}
scrollTimeout() {
this.timeout = setTimeout(() => {
this.onScrollEnd()
}, 120)
}
watchScrollPosition() {
if (this.isHorizontal) this.watchX()
if (this.isVertical) this.watchY()
}
getY() {
return this.isWindow ? window.scrollY : this.el.scrollTop
}
getYMax() {
let max = null
if (this.isWindow) {
max = Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.documentElement.clientHeight
)
} else {
max = Math.max(
this.el.scrollHeight,
this.el.offsetHeight,
this.el.clientHeight
)
}
return max - window.innerHeight
}
isMaxY() {
return this.getY() >= this.getYMax()
}
isMinY() {
return this.getY() <= 0
}
getX() {
return this.isWindow ? window.scrollX : this.el.scrollLeft
}
getXMax() {
let max = null
if (this.isWindow) {
max = Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
)
} else {
max = Math.max(
this.el.scrollWidth,
this.el.offsetWidth,
this.el.clientWidth
)
}
return max - window.innerWidth
}
isMaxX() {
return this.getX() >= this.getXMax()
}
isMinX() {
return this.getX() <= 0
}
watchX() {
const x = this.getX()
const scrolling = x !== this.x
const scrollingLeft = x < this.x
const scrollingRight = x > this.x
const atEnd = x !== this.x && this.isMaxX()
const atStart = x !== this.x && this.isMinX()
this.x = x
if (scrolling) {
this.onScrollStart()
if (scrollingLeft && !this.scrollingL) {
this.dispatch('scrollLeft')
this.scrollingL = 1
this.scrollingR = 0
} else if (scrollingRight && !this.scrollingR) {
this.dispatch('scrollRight')
this.scrollingL = 0
this.scrollingR = 1
}
}
if (atStart) this.dispatch('scrollMinX')
if (atEnd) this.dispatch('scrollMaxX')
if (this.x) this.dispatch('scrollX')
}
watchY() {
const y = this.getY()
const scrolling = y !== this.y
const scrollingUp = y < this.y
const scrollingDown = y > this.y
const atEnd = y !== this.y && this.isMaxY()
const atStart = y !== this.y && this.isMinY()
this.y = y
if (scrolling) {
this.onScrollStart()
if (scrollingDown && !this.scrollingD) {
this.dispatch('scrollDown')
this.scrollingD = 1
this.scrollingU = 0
} else if (scrollingUp && !this.scrollingU) {
this.dispatch('scrollUp')
this.scrollingD = 0
this.scrollingU = 1
}
}
if (atStart) this.dispatch('scrollMinY')
if (atEnd) this.dispatch('scrollMaxY')
if (this.y) this.dispatch('scrollY')
}
dispatch(type) {
const isValidOverride =
typeof this.events === 'object' && type in this.events
const isValidDefault =
Array.isArray(this.events) && this.events.includes(type)
const unthrottledEvents = ['scrollX', 'scrollY']
const eventNotDuplicated = this.lastDispatch !== type
// start/stop/direction events fire only once
if (eventNotDuplicated && (isValidOverride || isValidDefault)) {
this.el.dispatchEvent(
new CustomEvent(type, {
detail: {
x: this.x,
y: this.y,
},
})
)
this.lastDispatch = type
if (this.debugMode) console.info(type)
}
// updates to x or y fire each time
if (unthrottledEvents.includes(type)) {
this.el.dispatchEvent(
new CustomEvent(type, {
detail: {
x: this.x,
y: this.y,
},
})
)
if (this.debugMode) console.info(type)
}
}
onScroll() {
if (this.rafKilled) return
this.rafId = window.requestAnimationFrame(() => {
this.watchScrollPosition()
// refresh timeout, watch for scroll stop
window.clearTimeout(this.timeout)
this.scrollTimeout()
})
}
onScrollStart() {
if (!this.isScrolling && this.hasInit) {
this.dispatch('scrollStart')
this.isScrolling = true
}
this.hasScrolled = 1
}
onScrollEnd() {
this.dispatch('scrollStop')
this.isScrolling = false
this.scrollingU = false
this.scrollingD = false
this.scrollingL = false
this.scrollingR = false
}
}