Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
Load all images when intersection observer is not supported

I'm using intersection observer for late loading images. How can I load all images in browsers that don't support intersection observer?

My script=

const imob = new IntersectionObserver((entries, self) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            llo(entry.target);
            self.unobserve(entry.target);
        }
    });
});
document.querySelectorAll('.lzyp').forEach((pcu) => {
    imob.observe(pcu);
});

const llo = (pcu) => {
    const img = pcu.querySelector('img');
    const sce = pcu.querySelectorAll('source');

    sce.forEach((sue) => {
        sue.srcset = sue.dataset.srcset;
        sue.removeAttribute('data-srcset');
    });
    img.src = img.dataset.src;
    img.removeAttribute('data-src');
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. Maybe a polyfill for IntersectionObserver?

  2. Adding fallback code if IntersectionObserver is not supported.

if (!('IntersectionObserver' in window) ||
    !('IntersectionObserverEntry' in window) ||
    !('intersectionRatio' in window.IntersectionObserverEntry.prototype) ||
    !('isIntersecting' in window.IntersectionObserverEntry.prototype)
) {
    // load all images here
    document.querySelectorAll('.lzyp').forEach( llo );
}

Resolve Arrow function is not supported in IE:

// change from this
document.querySelectorAll('.lzyp').forEach((pcu) => {
    imob.observe(pcu);
});

// to this
document.querySelectorAll('.lzyp').forEach( function(pcu) {
    imob.observe(pcu);
});

put together:

function llo(pcu) {
    const img = pcu.querySelector('img');
    const sce = pcu.querySelectorAll('source');

    sce.forEach((sue) => {
        sue.srcset = sue.dataset.srcset;
        sue.removeAttribute('data-srcset');
    });
    img.src = img.dataset.src;
    img.removeAttribute('data-src');
}

// IntersectionObserver is not supported
if (!('IntersectionObserver' in window) ||
    !('IntersectionObserverEntry' in window) ||
    !('intersectionRatio' in window.IntersectionObserverEntry.prototype) ||
    !('isIntersecting' in window.IntersectionObserverEntry.prototype)
) {
    // load all images here
    document.querySelectorAll('.lzyp').forEach( llo );
} else {
    const imob = new IntersectionObserver( function(entries, self) {
        entries.forEach( function(entry) {
            if (entry.isIntersecting) {
                llo(entry.target);
                self.unobserve(entry.target);
            }
        });
    });
    document.querySelectorAll('.lzyp').forEach(function(pcu) {
        imob.observe(pcu);
    });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda