forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll-fire.js
More file actions
64 lines (57 loc) · 1.77 KB
/
scroll-fire.js
File metadata and controls
64 lines (57 loc) · 1.77 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
import { debounce } from '../utils/debounce'
import { height, offset } from '../utils/dom'
import { getScrollTarget } from '../utils/scroll'
import { listenOpts } from '../utils/event'
function updateBinding (el, binding) {
const ctx = el.__qscrollfire
if (typeof binding.value !== 'function') {
ctx.scrollTarget.removeEventListener('scroll', ctx.scroll)
console.error('v-scroll-fire requires a function as parameter', el)
return
}
ctx.handler = binding.value
if (typeof binding.oldValue !== 'function') {
ctx.scrollTarget.addEventListener('scroll', ctx.scroll, listenOpts.passive)
ctx.scroll()
}
}
export default {
name: 'scroll-fire',
bind (el, binding) {
let ctx = {
scroll: debounce(() => {
let containerBottom, elementBottom, fire
if (ctx.scrollTarget === window) {
elementBottom = el.getBoundingClientRect().bottom
fire = elementBottom < window.innerHeight
}
else {
containerBottom = offset(ctx.scrollTarget).top + height(ctx.scrollTarget)
elementBottom = offset(el).top + height(el)
fire = elementBottom < containerBottom
}
if (fire) {
ctx.scrollTarget.removeEventListener('scroll', ctx.scroll, listenOpts.passive)
ctx.handler(el)
}
}, 25)
}
el.__qscrollfire = ctx
},
inserted (el, binding) {
let ctx = el.__qscrollfire
ctx.scrollTarget = getScrollTarget(el)
updateBinding(el, binding)
},
update (el, binding) {
if (binding.value !== binding.oldValue) {
updateBinding(el, binding)
}
},
unbind (el) {
let ctx = el.__qscrollfire
if (!ctx) { return }
ctx.scrollTarget.removeEventListener('scroll', ctx.scroll, listenOpts.passive)
delete el.__qscrollfire
}
}