Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!