I’m working on a custom lazy load script which will lazy load images and then add a custom class to a parent element which then triggers all of the custom css animations. I had it working perfectly, so I thought. But when I started loading the page with not all the images in the viewport on load, nothing happens to that element.
(() => {
const initLazyMask = () => {
const isImgInViewport = (img) => {
const rect = img.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
};
const lazyMaskLoader = () => {
const lazyParents = document.querySelectorAll('[data-lazy-mask]');
let delay = 0;
lazyParents.forEach((parent, i) => {
const img = parent.querySelector('img');
function add_mask(e, c) {
m = document.createElement(e);
m.classList.add(c);
parent.appendChild(m) ;
}
This is my code, which is in current state giving me codepen errors, but I’m hoping this won’t be the case much longer.
Link to the codepen is here :
Codepen Link
Any and all help appreciated.