-
Notifications
You must be signed in to change notification settings - Fork 4
/
multipleelements.html
65 lines (49 loc) · 1.59 KB
/
multipleelements.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Multiple elements IntersectionObserver Example</title>
<link rel="stylesheet" href="style.css" />
</head>
<body class="multiple">
<div class="status status--invisible">No box</div>
<!-- These elements visiblity gets observed -->
<div class="box box1">Box 1</div>
<div class="box box2">Box 2</div>
<div class="box box3">Box 3</div>
<script>
let visiblity = 'invisible';
let statusText = 'No box';
// 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';
statusText = entries[0].target.textContent;
// Element leaves the viewport
} else {
visiblity = 'invisible';
statusText = 'No box';
}
updateStatus(visiblity, statusText);
});
// Elements to be observed
const box1 = document.querySelector('.box1');
const box2 = document.querySelector('.box2');
const box3 = document.querySelector('.box3');
// Start observing .box
io.observe(box1);
io.observe(box2);
io.observe(box3);
// Just necessary for displaying the current status
function updateStatus(visiblity, statusText) {
console.log(visiblity);
const status = document.querySelector('.status');
status.textContent = statusText;
status.className = 'status status--' + visiblity;
}
</script>
</body>
</html>