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

225
Visualizações
Have background images of a carousel lazy-load and not be considered essential for UX purposes

I have a simple carousel on my home page. It changes pictures automatically after a couple of seconds. The html looks like this:

<div>
    <div id="img-1" class="carousel-image" style="display: block;"></div>
    <div id="img-2" class="carousel-image" style="display: none;"></div>
    <div id="img-3" class="carousel-image" style="display: none;"></div>        
</div>

I use a javascript function to add the images as background-image. To make sure the carousel images are all loaded before the carousel starts changing automatically I use new Image() and count the number of loaded images. Once all three images are loaded I can do startCarousel().

var numberOfLoadedImages = 0;
function setImage(imgUrl, imgId){
    var homeImage = document.getElementById(imgId);
    homeImage.style.backgroundImage = 'url(' + imgUrl + ")";
    var img = new Image();
    img.onload = function() {
        if(numberOfLoadedImages === 2){
            startCarousel();
        }
        numberOfLoadedImages++;
    }
    img.src = imgUrl;
    if (img.complete) {
        img.onload();
    }
}

I can then simple call this function like this:

setImage('img-1', 'img-1');
setImage('img-2', 'img-2');
setImage('img-3', 'img-3');

What I would like to achieve is to lazy-load the second and third image. These two are not necessary at first page load; only the first image is important. Google Page Speed Insights mentions the second and third under the heading "Serve images in next-gen formats", whereas, if you ask me, they should not be considered part of the page. They should be loading in the background and they are really not essential for the first display and UX.

How can I tell the browser (or Google Page Speed Insights) that these images are completely non-essential for UX purposes and should not be considered part of the first paint and UX?

I tried putting the second and third call in a timeout with a short interval, but that didn't do the trick.

NB: I am using vanilla javascript; no plugins at all.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can wait for everything to load and once the page is fully loaded (including images, etc.) you can run setImage for your second and third image.

document.addEventListener('DOMContentLoaded', function() {
   setImage('img-2', 'img-2');
   setImage('img-3', 'img-3');
}, false);

In case Google Page Speed Insights still registers this and prolong the initial page load time you can use async function to call setImage.

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