I built a simple infinite scroll using the intersection observer API, would love some feedback on ways to improve my JavaScript and make it more maintainable and reusable, and feedback according to best JavaScript practices.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Infinite Scroll</title>
<style>
body {
font-family: sans-serif;
font-size: 16px;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
max-width: 520px;
margin: 0 auto;
}
img {
width: 100%;
height: 100%;
margin: 1rem 2rem;
}
#load-more {
padding: 1rem 2rem;
margin: 2;
background-color: darkcyan;
color: white;
border-radius: 1rem;
}
</style>
</head>
<body>
<div class="container">
<div id="infinite-scroll-imgs-container"></div>
<div id="load-more">Loading more content...</div>
</div>
<script>
const loadMore = document.querySelector("#load-more");
const observer = new IntersectionObserver(handleIntersect, { threshold: 1 });
const imgsContainer = document.querySelector("#infinite-scroll-imgs-container");
observer.observe(loadMore)
function randomInteger() {
return Math.floor(Math.random() * 1000000);
}
function imgGenerator(images) {
const imgs = [];
for (let i = 0; i < images; i += 1) {
const img = document.createElement("img");
img.src = `http://api.adorable.io/avatars/${randomInteger()}`;
img.classList.add('infinite-scroll-img')
imgs.push(img);
}
return imgs;
}
function appendImages(container, images) {
container.append(...imgGenerator(images));
}
function handleIntersect(entries, observer) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
appendImages(imgsContainer, 5);
}
});
}
appendImages(imgsContainer, 5);
</script>
</body>
</html>