forked from zel777/easeScroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.es.easeScroll.js
445 lines (408 loc) · 13 KB
/
module.es.easeScroll.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
const EaseScroll = function() {
// Define option defaults
const defaults = {
frameRate: 60,
animationTime: 1000,
stepSize: 120,
pulseAlgorithm: true,
pulseScale: 8,
pulseNormalize: 1,
accelerationDelta: 20,
accelerationMax: 1,
keyboardSupport: true,
arrowScroll: 50,
touchpadSupport: true,
fixedBackground: true,
disabledClass: null, // if body has this class will stop scroll, type: string: 'className' or Array ['class1', 'class2']
browser: {
Chrome: true,
FireFox: true,
Safari: true,
IE: true,
Edge: true
}
}
this.options = { ...defaults, ...arguments[0] }
this.options.browser = { ...defaults.browser, ...arguments[0].browser }
esSettings = this.options
currentBrowserBuildAllow = esSettings.browser[currentBrowser]
init()
}
// -----------------
// Public Methods
EaseScroll.prototype.destroy = destroy
EaseScroll.prototype.build = build
// -----------------
// Private Methods
function init() {
if (!currentBrowserBuildAllow) return false
addEventListener('mousedown', mouseDownHandler)
addEventListener(wheelEvent, mouseWheelHandler, { passive: false })
addEventListener('load', loadedHandler)
}
function destroy() {
if (!currentBrowserBuildAllow) return false
removeEventListener('mousedown', mouseDownHandler)
removeEventListener(wheelEvent, mouseWheelHandler)
removeEventListener('keydown', keyDownHandler)
isDestroy = true
loaded = false
}
function build() {
if (isDestroy) init()
}
function keyListenerHandler() {
if (esSettings.keyboardSupport) addEventListener('keydown', keyDownHandler)
}
function loadedHandler() {
if (document.body) {
const html = document.documentElement
const body = document.body
const windowHeight = window.innerHeight
const bodyHeight = body.scrollHeight
if (
((scrollMainEl = document.compatMode.indexOf('CSS') >= 0 ? html : body),
(currentHoverTarget = body),
keyListenerHandler(),
(loaded = true),
top !== self)
)
isInIframe = true
else if (
bodyHeight > windowHeight &&
(body.offsetHeight <= windowHeight || html.offsetHeight <= windowHeight)
) {
let a = false
const i = function() {
if (!a && html.scrollHeight !== document.height) {
a = true
setTimeout(function() {
html.style.height = document.height + 'px'
a = false
}, 100)
}
}
if (((html.style.height = 'auto'), setTimeout(i, 10), scrollMainEl.offsetHeight <= windowHeight)) {
const div = document.createElement('div')
div.style.clear = 'both'
body.appendChild(div)
}
}
if (!esSettings.fixedBackground) {
body.style.backgroundAttachment = 'scroll'
html.style.backgroundAttachment = 'scroll'
}
}
}
function scrollControlHandler(scrollTarget, scrollX, scrollY, ms) {
if ((ms || (ms = 1000), resetScrollControl(scrollX, scrollY), esSettings.accelerationMax !== 1)) {
const r = +new Date()
const a = r - C
if (a < esSettings.accelerationDelta) {
let i = (1 + 30 / a) / 2
if (i > 1) {
i = Math.min(i, esSettings.accelerationMax)
scrollX *= i
scrollY *= i
}
}
C = +new Date()
}
if (
(M.push({
x: scrollX,
y: scrollY,
lastX: scrollX < 0 ? 0.99 : -0.99,
lastY: scrollY < 0 ? 0.99 : -0.99,
start: +new Date()
}),
!animationRunning)
) {
const isBody = scrollTarget === document.body
const step = function() {
const timestamp = +new Date()
let xCoord = 0
let yCoord = 0
for (let c = 0; c < M.length; c++) {
const scrollSet = M[c]
const progress = timestamp - scrollSet.start
const isAnimationEnd = progress >= esSettings.animationTime
let h = isAnimationEnd ? 1 : progress / esSettings.animationTime
esSettings.pulseAlgorithm && (h = p(h))
const m = (scrollSet.x * h - scrollSet.lastX) >> 0
const w = (scrollSet.y * h - scrollSet.lastY) >> 0
xCoord += m
yCoord += w
scrollSet.lastX += m
scrollSet.lastY += w
if (isAnimationEnd) {
M.splice(c, 1)
c--
}
}
if (isBody) window.scrollBy(xCoord, yCoord)
else {
if (xCoord) scrollTarget.scrollLeft += xCoord
if (yCoord) scrollTarget.scrollTop += yCoord
}
if (!scrollX && !scrollY) M = []
if (M.length) requestAnimationFrame(step, scrollTarget, ms / esSettings.frameRate + 1)
else animationRunning = false
}
requestAnimationFrame(step, scrollTarget, 0)
animationRunning = true
}
}
function mouseWheelHandler(event) {
loaded || loadedHandler()
if (scrollDisabled()) { return event.preventDefault() }
const mouseWheelTarget = event.target
const scrollTarget = l(mouseWheelTarget)
if (
!scrollTarget ||
event.defaultPrevented ||
checkTagName(currentHoverTarget, 'embed') ||
(checkTagName(mouseWheelTarget, 'embed') && /\.pdf/i.test(mouseWheelTarget.src))
)
return true
let scrollX = event.wheelDeltaX || -event.deltaX * 40 || 0
let scrollY = event.wheelDeltaY || -event.deltaY * 40 || 0
return (
scrollX || scrollY || (scrollY = event.wheelDelta || 0),
!esSettings.touchpadSupport && f(scrollY)
? true
: (Math.abs(scrollX) > 1.2 && (scrollX *= esSettings.stepSize / 120),
Math.abs(scrollY) > 1.2 && (scrollY *= esSettings.stepSize / 120),
scrollControlHandler(scrollTarget, -scrollX, -scrollY),
event.preventDefault())
)
}
function keyDownHandler(event) {
const target = event.target
const isControlKeyboard =
event.ctrlKey || event.altKey || event.metaKey || (event.shiftKey && event.keyCode !== key.spacebar)
if (scrollDisabled()) { return event.preventDefault() }
if (
/input|textarea|select|embed/i.test(target.nodeName) ||
target.isContentEditable ||
event.defaultPrevented ||
isControlKeyboard
)
return true
if (checkTagName(target, 'button') && event.keyCode === key.spacebar) return true
let direction
let scrollX = 0
let scrollY = 0
const scrollTarget = l(currentHoverTarget) || document.body
let scrollTargetHeight = scrollTarget.clientHeight
switch ((scrollTarget === document.body && (scrollTargetHeight = window.innerHeight), event.keyCode)) {
case key.up:
scrollY = -esSettings.arrowScroll
break
case key.down:
scrollY = esSettings.arrowScroll
break
case key.spacebar:
direction = event.shiftKey ? 1 : -1
scrollY = -direction * scrollTargetHeight * 0.9
break
case key.pageup:
scrollY = 0.9 * -scrollTargetHeight
break
case key.pagedown:
scrollY = 0.9 * scrollTargetHeight
break
case key.home:
scrollY = scrollTarget !== document.body ? -scrollTarget.scrollTop : -document.documentElement.scrollTop
break
case key.end: {
const scrollTop =
scrollTarget !== document.body ? scrollTarget.scrollTop : document.documentElement.scrollTop
const distance = scrollTarget.scrollHeight - scrollTop - scrollTargetHeight
scrollY = distance > 0 ? distance + 10 : 0
break
}
case key.left:
scrollX = -esSettings.arrowScroll
break
case key.right:
scrollX = esSettings.arrowScroll
break
default:
return true
}
scrollControlHandler(scrollTarget, scrollX, scrollY)
event.preventDefault()
}
function mouseDownHandler(event) {
currentHoverTarget = event.target
}
function scrollDisabled() {
if (esSettings.disabledClass && esSettings.disabledClass !== '') {
if (typeof esSettings.disabledClass === 'string') {
if (document.body.classList.contains(esSettings.disabledClass)) {
return true
}
} else if (typeof esSettings.disabledClass === 'object') {
for(var i = 0; i < esSettings.disabledClass.length; i++) {
if (document.body.classList.contains(esSettings.disabledClass[i])) {
return true
}
}
}
}
return false
}
function i(e, t) {
for (let o = e.length; o--; ) z[N(e[o])] = t
return t
}
function l(el) {
const t = []
const o = scrollMainEl.scrollHeight
do {
const n = z[N(el)]
let overflow
if (n) return i(t, n)
if ((t.push(el), o === el.scrollHeight)) {
if (!isInIframe || scrollMainEl.clientHeight + 10 < o) return i(t, document.body)
} else if (
el.clientHeight + 10 < el.scrollHeight &&
((overflow = getComputedStyle(el, '').getPropertyValue('overflow-y')),
overflow === 'scroll' || overflow === 'auto')
)
return i(t, el)
} while ((el = el.parentNode))
}
function addEventListener(eventName, func, options) {
window.addEventListener(eventName, func, options || false)
}
function removeEventListener(eventName, func, options) {
window.removeEventListener(eventName, func)
}
function checkTagName(target, tagName) {
return (target.nodeName || '').toLowerCase() === tagName.toLowerCase()
}
function resetScrollControl(scrollX, scrollY) {
scrollX = scrollX > 0 ? 1 : -1
scrollY = scrollY > 0 ? 1 : -1
if (scrollDir.x !== scrollX || scrollDir.y !== scrollY) {
scrollDir.x = scrollX
scrollDir.y = scrollY
M = []
C = 0
}
}
function f(scrollY) {
if (scrollY) {
scrollY = Math.abs(scrollY)
D.push(scrollY)
D.shift()
clearTimeout(timer)
const t = D[0] === D[1] && D[1] === D[2]
const o = h(D[0], 120) && h(D[1], 120) && h(D[2], 120)
return !(t || o)
}
}
function h(e, t) {
return Math.floor(e / t) === e / t
}
function m(e) {
let t, o, n
return (
(e *= esSettings.pulseScale),
e < 1
? (t = e - (1 - Math.exp(-e)))
: ((o = Math.exp(-1)), (e -= 1), (n = 1 - Math.exp(-e)), (t = o + n * (1 - o))),
t * esSettings.pulseNormalize
)
}
function p(e) {
return e >= 1 ? 1 : e <= 0 ? 0 : (esSettings.pulseNormalize === 1 && (esSettings.pulseNormalize /= m(1)), m(e))
}
let currentHoverTarget
let esSettings = null
let isInIframe = false
const scrollDir = {
x: 0,
y: 0
}
let loaded = false
let isDestroy = false
let scrollMainEl = document.documentElement
const D = [120, 120, 120]
const key = {
left: 37,
up: 38,
right: 39,
down: 40,
spacebar: 32,
pageup: 33,
pagedown: 34,
end: 35,
home: 36
}
let M = []
let animationRunning = false
let C = +new Date()
let z = {}
setInterval(function() {
z = {}
}, 10000)
let timer
const N = function() {
let e = 0
return function(t) {
return t.uniqueID || (t.uniqueID = e++)
}
}
const requestAnimationFrame =
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, target, delay) {
window.setTimeout(callback, delay || 1000 / 60)
}
window.requestAnimationFrame = requestAnimationFrame
const wheelEvent =
'onwheel' in document
? // spec event type
'wheel'
: document.onmousewheel !== undefined
? // legacy event type
'mousewheel'
: // older Firefox
'DOMMouseScroll'
// Browser Detect
function getBrowser() {
let browser = 'unknown';
const ua = navigator.userAgent;
if ((ua.indexOf("Opera") || ua.indexOf('OPR')) !== -1) {
browser = 'Opera'
}
else if (ua.indexOf("Edge") != -1) {
browser = 'Edge'
}
else if (ua.indexOf("Chrome") != -1) {
browser = 'Chrome'
}
else if (ua.indexOf("Safari") != -1) {
browser = 'Safari'
}
else if (ua.indexOf("Firefox") != -1) {
browser = 'FireFox'
}
else if ((ua.indexOf("MSIE") != -1) || (!!document.documentMode === true)) //IF IE > 10
{
browser = 'IE'
}
else {
browser = 'unknown'
}
return browser;
}
const currentBrowser = getBrowser();
let currentBrowserBuildAllow = false
// [End] Browser Detect
export default EaseScroll