-
Notifications
You must be signed in to change notification settings - Fork 4
/
threshold.html
67 lines (49 loc) · 1.67 KB
/
threshold.html
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
65
66
67
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Threshold IntersectionObserver Example</title>
<link rel="stylesheet" href="style.css" />
</head>
<body class="threshold">
<div class="status status--invisible">invisible</div>
<!-- This elements visiblity gets observed -->
<div class="box"></div>
<script>
let visiblity = 'invisible';
let intersectionRatio = 0;
// Create new IntersectionObserver
const io = new IntersectionObserver(entries => {
// Available data when an intersection happens
console.log(entries);
// Element enters the viewport
if(entries[0].intersectionRatio !== 0) {
visiblity = 'visible';
// How much of the element is visible
intersectionRatio = entries[0].intersectionRatio;
// Element leaves the viewport
} else {
visiblity = 'invisible';
intersectionRatio = 0;
}
updateStatus(visiblity, intersectionRatio);
}, {
// Call the observer, when the element enters the viewport,
// when 25%, 50%, 75% and the whole element are visible
threshold: [0, 0.25, 0.5, 0.75, 1]
});
// Element to be observed
const box = document.querySelector('.box');
// Start observing .box
io.observe(box);
// Just necessary for displaying the current status
function updateStatus(visiblity, threshold) {
console.log(visiblity);
const status = document.querySelector('.status');
const percent = (threshold * 100).toFixed(2);
status.textContent = `${percent}% ${visiblity}`;
status.className = 'status status--' + visiblity;
}
</script>
</body>
</html>