-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (26 loc) · 960 Bytes
/
script.js
File metadata and controls
33 lines (26 loc) · 960 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
33
let isIntersecting = false
let timeoutID
// check that the target remains visible for at least one second
function handleChange(bool) {
isIntersecting = bool
clearTimeout(timeoutID)
timeoutID = setTimeout(() => {
if (isIntersecting && document.hasFocus()) {
console.log('Target Seen')
}
}, 1000)
}
// observe for intersection only if document is visible
function handleTabVisibility() {
if (document.visibilityState === "hidden") {
observer.unobserve(document.querySelector(".target"));
} else {
observer.observe(document.querySelector(".target"));
}
}
// target div has to be at least 50% of its size visible
const observer = new IntersectionObserver(entries => handleChange(entries[0].isIntersecting), { threshold: [0.5] });
document.addEventListener("visibilitychange", handleTabVisibility, false);
window.onload = () => {
observer.observe(document.querySelector(".onetag"));
}