Skip to content

Instantly share code, notes, and snippets.

@surma
Last active February 15, 2018 12:39
Show Gist options
  • Save surma/0cb090b8764598d5aa002356f6c9b0d1 to your computer and use it in GitHub Desktop.
Save surma/0cb090b8764598d5aa002356f6c9b0d1 to your computer and use it in GitHub Desktop.
<!doctype html>
<style>
html, body {
margin: 0;
background-color: #CCC;
box-sizing: border-box;
}
.item {
margin: 20px;
padding: 20px;
background-color: #FFF;
border: 1px solid #666;
}
.item p {
width: 100%;
background-color: #666;
height: 10px;
}
#template {
display: none;
}
#sentinel {
width: 1px;
height: 1px;
}
</style>
<div id="template" class="item">
<p></p>
<p></p>
<p></p>
</div>
<div id="sentinel"></div>
<script>
var template = document.getElementById('template');
var sentinel = document.getElementById('sentinel');
function loadItems(n) {
for(var i = 0; i < n; i++) {
var newItem = template.cloneNode(true);
newItem.id = '';
document.body.appendChild(newItem);
}
}
var io = new IntersectionObserver(_ => {
sentinel.parentNode.removeChild(sentinel);
requestAnimationFrame(_ => {
loadItems(10);
document.body.appendChild(sentinel);
loadItems(1);
});
});
io.observe(sentinel);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment