-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (39 loc) · 1.37 KB
/
index.js
File metadata and controls
44 lines (39 loc) · 1.37 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
/**
*
* @param {String | String[]} selector css selector or array of css selector
* @param {Object} option options for scrollObserver
*/
export default function scrollObserver(selector, option) {
let observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
window.requestIdleCallback(() => {
let showCount = 0
if (option?.once) {
if (showCount === 0 && entry.isIntersecting) {
entry.target.classList.add('shown')
option.onshow ? option.onshow(entry) : false
showCount++
}
}
else {
if (entry.isIntersecting) {
entry.target.classList.add('shown')
if (option && option.onshow) option.onshow(entry)
}
else {
entry.target.classList.remove('shown')
if (option && option.onhide) option.onhide(entry)
}
}
})
})
}, option)
if (Array.isArray(selector))
selector.forEach(qAll)
else
qAll(selector)
function qAll(selector) {
const item = document.querySelectorAll(selector)
item.forEach(i => observer.observe(i))
}
}