Skip to content

Commit

Permalink
new lazyload for images
Browse files Browse the repository at this point in the history
  • Loading branch information
amgno committed Sep 25, 2024
1 parent e8f4404 commit 76f9be5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
29 changes: 28 additions & 1 deletion project.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
#new-img-div {
column-count: 1;
}

.lazy-image {
opacity: 0;
transition: opacity 0.3s;
}

.lazy-image[src] {
opacity: 1;
}
}
</style>
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
Expand Down Expand Up @@ -121,9 +130,11 @@
if (project.images && project.images.length > 0) {
project.images.forEach((imageUrl) => {
const img = document.createElement('img');
img.src = imageUrl;
img.dataset.src = imageUrl; // Use data-src instead of src
img.className = 'lazy-image'; // Add a class for lazy loading
imageContainer.appendChild(img);
});
lazyLoadImages(); // Call a function to handle lazy loading
} else {
console.log("no images, removing div");
document.getElementById('image-gallery').style.display = 'none';
Expand All @@ -139,6 +150,22 @@
document.getElementById('project-content').innerHTML = '<p>Project not found.</p>';
}
});

function lazyLoadImages() {
const lazyImages = document.querySelectorAll('img.lazy-image');
const imageObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove('lazy-image');
imageObserver.unobserve(img);
}
});
});

lazyImages.forEach(img => imageObserver.observe(img));
}
</script>

<script src="./transition.js"></script>
Expand Down
9 changes: 4 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
## List to remind me of things to do

## In progress
• Lazy load images <br>
• Add a progress bar for uploading images <br>
• Fix the size of buttons on works for mobile view <br>
• fix layout of the admin dashboard <br>
• After deleting a project, and trying to open a existing one it gives an error <br>


## Done

• Fix delete function on image manager <br>
✅ Fix delete function on image manager <br>
✅ Add a progress bar for uploading images <br>
✅ fix layout of the admin dashboard <br>
✅ Lazy load images <br>

0 comments on commit 76f9be5

Please sign in to comment.