silvester-23/static/lazy.js

20 lines
603 B
JavaScript
Raw Normal View History

2023-11-20 16:51:33 +01:00
window.onload = function () {
// Get all <img> elements with loading="lazy"
var lazyImages = document.querySelectorAll('img[loading="lazy"]');
2023-11-20 16:16:23 +01:00
2023-11-20 16:51:33 +01:00
// Loop through each image and apply the fade-in effect
lazyImages.forEach(function (lazyImage) {
lazyImage.style.opacity = 0;
lazyImage.classList.add("lazy-image")
2023-11-20 16:54:05 +01:00
console.log(lazyImage)
2023-11-20 16:16:23 +01:00
2023-11-20 16:51:33 +01:00
// Create an image element to check when it's loaded
var img = new Image();
img.src = lazyImage.src;
img.onload = function () {
2023-11-20 16:54:05 +01:00
console.log("lloooaded")
2023-11-20 16:51:33 +01:00
lazyImage.style.opacity = 1;
};
});
};