Skip to content

Commit

Permalink
Prioritizing Image Loading
Browse files Browse the repository at this point in the history
Prioritized image loading in js
  • Loading branch information
shreyad2806 committed Oct 2, 2024
1 parent 7c48602 commit 8bbc62e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,30 @@ orderBtn.addEventListener("click", ()=>{
alert("Order placed!");
}
})

// Prioritizing Image Loading
<script>
// Critical images
const criticalImages = document.querySelectorAll('.critical-image');

// Lazy load other images
const lazyImages = document.querySelectorAll('img[data-src]');

// Load critical images immediately
criticalImages.forEach(image => {
image.src = image.dataset.src;
});

// Use Intersection Observer for lazy loading
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.src = entry.target.dataset.src;
observer.unobserve(entry.target);
}
});
});

lazyImages.forEach(image => observer.observe(image));
</script>

0 comments on commit 8bbc62e

Please sign in to comment.