What are the potential pitfalls of loading all images for pagination at once using JavaScript in PHP?

Loading all images for pagination at once can lead to slow page load times and increased server load, especially if there are a large number of images. To solve this issue, it's better to implement lazy loading, where images are loaded only when they are about to be displayed on the page.

// Implement lazy loading for images in pagination
// Example code using JavaScript to load images only when they are about to be displayed

echo '<div class="image-container">';
for ($i = 1; $i <= $totalImages; $i++) {
    echo '<img src="placeholder.jpg" data-src="image'.$i.'.jpg" class="lazy-image">';
}
echo '</div>';

echo '<script>
document.addEventListener("DOMContentLoaded", function() {
    const lazyImages = document.querySelectorAll(".lazy-image");

    const lazyLoad = function() {
        lazyImages.forEach(function(image) {
            if (image.getBoundingClientRect().top < window.innerHeight) {
                image.src = image.getAttribute("data-src");
                image.classList.remove("lazy-image");
            }
        });
    };

    lazyLoad();

    document.addEventListener("scroll", lazyLoad);
});
</script>';