-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple.html
56 lines (40 loc) · 1.22 KB
/
simple.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple IntersectionObserver Example</title>
<link rel="stylesheet" href="style.css" />
</head>
<body class="simple">
<div class="status status--invisible">invisible</div>
<!-- This elements visiblity gets observed -->
<div class="box"></div>
<script>
let visiblity = 'invisible';
// 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';
// Element leaves the viewport
} else {
visiblity = 'invisible';
}
updateStatus(visiblity);
});
// 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) {
console.log(visiblity);
const status = document.querySelector('.status');
status.textContent = visiblity;
status.className = 'status status--' + visiblity;
}
</script>
</body>
</html>