Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (40 sloc) 1.22 KB
<!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>