Skip to content

Instantly share code, notes, and snippets.

@nikhilkesari
Created November 7, 2017 06:16
Show Gist options
  • Select an option

  • Save nikhilkesari/14d48282e8afe7c6bb9c5543ff954e7b to your computer and use it in GitHub Desktop.

Select an option

Save nikhilkesari/14d48282e8afe7c6bb9c5543ff954e7b to your computer and use it in GitHub Desktop.
Lazy load using IntersectionObserver
import '../css/main.css';
console.log("message");
(function() {
var observer;
var index = 0;
var options = {
root: null,
rootMargin: '0px',
threshold: 0
}
function addNewBoxes(entry) {
entry.forEach((change) => {
if (change.isIntersecting && index <= 10) {
console.log(index);
index = ++index;
createNewBox(index);
}
if (index === 10) {
observer.disconnect();
}
});
}
function createNewBox(index) {
var newDiv = document.createElement("div");
var newContent = document.createTextNode(index);
newDiv.setAttribute('class', 'bigbox');
newDiv.appendChild(newContent);
document.getElementById("main").appendChild(newDiv);
observer.observe(newDiv);
}
observer = new IntersectionObserver(addNewBoxes, options);
createNewBox(0);
})();
<!DOCTYPE html>
<html>
<head>
<title>Hello Webpack</title>
</head>
<body>
<div id="top"></div>
<div id="main"></div>
<footer>
<div class="footer" id="footer">Footer</div>
</footer>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment