-
Notifications
You must be signed in to change notification settings - Fork 14
/
observer.js
30 lines (30 loc) · 915 Bytes
/
observer.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
export default function (
delay,
{ root = "", rootMargin = "", threshold = 0.25 } = {},
prefetch
) {
return prefetch
? `const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry, i) => {
entry.isIntersecting
? (prefetchTimeoutIDArray[i] = setTimeout(() => {
AstroSpa.prefetch(entry.target.href);
}, ${delay}))
: clearTimeout(prefetchTimeoutIDArray[i]);
});
},
{${
root && rootMargin
? `root: document.querySelector("${root}"), rootMargin: "${rootMargin}",`
: root
? `root: document.querySelector("${root}"),`
: rootMargin
? `rootMargin: "${rootMargin}",`
: ""
} threshold: ${
Array.isArray(threshold) ? `[${threshold}]` : threshold
} }
);`
: "";
}