Skip to content

Instantly share code, notes, and snippets.

@usrbowe
Last active May 29, 2017 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usrbowe/2b6663ea9dfb73514255da0a277d634a to your computer and use it in GitHub Desktop.
Save usrbowe/2b6663ea9dfb73514255da0a277d634a to your computer and use it in GitHub Desktop.
bug in intersection observer polyfill
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>IntersectionObserver</title>
<script src="https://cdn.polyfill.io/v2/polyfill.js?features=IntersectionObserver&min=false"></script>
<style>
html, body {
padding: 0;
margin: 0;
height: 100%;
}
body {
overflow-x: hidden;
}
.section {
height: 50vh;
background-color: #ccc;
}
.section:nth-child(odd) {
background-color: #ddd;
}
#box {
width: 400px;
height: 150px;
background-color: rebeccapurple;
}
</style>
</head>
<body>
<section class="section"></section>
<section class="section"></section>
<section class="section">
<!-- observed element -->
<div id="box"></div>
</section>
<section class="section"></section>
<script>
const iO = new IntersectionObserver((observedElements) => {
if (observedElements[0].intersectionRatio > 0) {
console.log('observe the element', observedElements[0].intersectionRatio);
}
}, {
threshold: [0.5],
});
iO.observe(document.getElementById('box'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment