forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollFire.js
More file actions
77 lines (65 loc) · 1.9 KB
/
ScrollFire.js
File metadata and controls
77 lines (65 loc) · 1.9 KB
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
import debounce from '../utils/debounce.js'
import { height, offset } from '../utils/dom.js'
import { getScrollTarget } from '../utils/scroll.js'
import { listenOpts } from '../utils/event.js'
function update (ctx, { value, oldValue }) {
if (typeof value !== 'function') {
ctx.scrollTarget.removeEventListener('scroll', ctx.scroll, listenOpts.passive)
return
}
ctx.handler = value
if (typeof oldValue !== 'function') {
ctx.scrollTarget.addEventListener('scroll', ctx.scroll, listenOpts.passive)
ctx.scroll()
}
}
function destroy (el) {
const ctx = el.__qscrollfire
if (ctx !== void 0) {
ctx.scrollTarget.removeEventListener('scroll', ctx.scroll, listenOpts.passive)
ctx.scroll.cancel()
delete el.__qscrollfire
}
}
export default {
name: 'scroll-fire',
inserted (el, binding) {
if (el.__qscrollfire !== void 0) {
destroy(el)
el.__qscrollfire_destroyed = true
}
const ctx = {
scrollTarget: getScrollTarget(el),
scroll: debounce(() => {
let containerBottom, elBottom
if (ctx.scrollTarget === window) {
elBottom = el.getBoundingClientRect().bottom
containerBottom = window.innerHeight
}
else {
elBottom = offset(el).top + height(el)
containerBottom = offset(ctx.scrollTarget).top + height(ctx.scrollTarget)
}
if (elBottom > 0 && elBottom < containerBottom) {
ctx.scrollTarget.removeEventListener('scroll', ctx.scroll, listenOpts.passive)
ctx.handler(el)
}
}, 25)
}
update(ctx, binding)
el.__qscrollfire = ctx
},
update (el, binding) {
if (el.__qscrollfire !== void 0 && binding.value !== binding.oldValue) {
update(el.__qscrollfire, binding)
}
},
unbind (el) {
if (el.__qscrollfire_destroyed === void 0) {
destroy(el)
}
else {
delete el.__qscrollfire_destroyed
}
}
}