Skip to content

Instantly share code, notes, and snippets.

@amasho
Created June 21, 2018 01:58
Show Gist options
  • Save amasho/24d0262fa6ce5cb42fd3f129c6ebbf83 to your computer and use it in GitHub Desktop.
Save amasho/24d0262fa6ce5cb42fd3f129c6ebbf83 to your computer and use it in GitHub Desktop.
IntersectionObserver demo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Intersection Observer DEMO | labs.jxck.io</title>
<style>
body {
width: 100%;
height: 1200px;
font-size: 2em;
}
#hoge {
margin: 1em;
width: 500px;
height: 50%;
background-color: #ccc;
box-sizing: border-box;
}
#pad1,
#pad2 {
margin: 1em;
width: 500px;
height: 200px;
background-color: #ccc;
}
</style>
</head>
<body>
<div id="hoge"></div>
<div id="pad1" style="display: none">1</div>
<div class="next-inner-page-link">
<input type="button">button</input>
</div>
<div id="pad2" style="display: none">2</div>
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
<script>
if (document.querySelector('.next-inner-page-link') !== null) {
const ob = new IntersectionObserver(function (entry, ob) {
for (var e of entry) {
if (!e.isIntersecting) return
setTimeout(() => {
document.getElementById('pad1').style.display = 'block';
document.getElementById('pad2').style.display = 'block';
ob.unobserve(e.target)
}, 1000);
}
}, {
threshold: [0, 0.5, 1.0]
});
ob.observe(document.querySelector('.next-inner-page-link'))
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment