Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (45 sloc) 1.45 KB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Different Root IntersectionObserver Example</title>
<link rel="stylesheet" href="style.css" />
</head>
<body class="root">
<div class="status status--invisible">invisible</div>
<div class="container">
<!-- This elements visiblity within its parent element gets observed -->
<div class="box"></div>
</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 visible area of its parent element
if(entries[0].intersectionRatio !== 0) {
visiblity = 'visible';
// Element leaves the visible area of its parent element
} else {
visiblity = 'invisible';
}
updateStatus(visiblity);
}, {
// Define an element as the root
root: document.querySelector('.container')
});
// 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>