-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersect.js
More file actions
32 lines (29 loc) · 878 Bytes
/
intersect.js
File metadata and controls
32 lines (29 loc) · 878 Bytes
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
const onIntersect = (entries, observer, el, binding) => {
for (const entry of entries) {
if (entry.isIntersecting) {
binding.value(entry, el)
}
}
}
const intersectDirective = {
beforeMount(el, binding) {
const options = {
root: binding.arg ? document.querySelector(binding.arg) : null,
rootMargin: '0px',
threshold: 0.01
}
el._vueIntersectObserver = new IntersectionObserver((entries, observer) => onIntersect(entries, observer, el, binding), options)
el._vueIntersectObserver.observe(el)
},
unmounted(el) {
if (el._vueIntersectObserver) {
el._vueIntersectObserver.disconnect()
delete el._vueIntersectObserver
}
}
}
export default {
install(app) {
app.directive('intersect', intersectDirective)
}
}