0

I have a lot of images on my page, and I am making lazy loading for them. I use img.onload = showmeup(img); combination for all the images, but they all appear simultaniusly, when all are loaded, not one after each other.

Could you give me an advise, how to fix it to let them appears one after each other.

This is how I retrieve images from the server (It is WordPress BTW):

<div id="postbody" class="postcontent">
   <?php the_content();?>
</div>

This is the JS code:

<script>

var div = document.getElementById("postbody"),
    images = div.getElementsByTagName("img");

function showmeup(obg){
  obg.style.animationName = "showmeup";
}

for (var i = 0; i < images.length; i++){
    images[i].onload = showmeup(images[i]);
}

</script>

Also I tired a more complicated way, using if (images[i].complete) {} to ensure that all images get processed apart:

<script>

var div = document.getElementById("postbody"),
    images = div.getElementsByTagName("img");

function showmeup(obg){
  obg.style.animationName = "showmeup";
}

var passed = [],
    ready = 0;

while (ready == 0){
  for (var i = 0; i < images.length; i++){
    if (images[i].complete && images[i].naturalHeight !== 0){
      if (passed.includes(i) == false){
        showmeup(images[i]);
        passed.push(i);
      }
    }
  }

  if (passed.length == images.length){
    ready = 1;
  }
}

</script>

But they still appear simultaniusly right after all images are loaded.

I tried to put JS code infront of </body> and into <footer></footer> and right after the <?php the_content();?>. But all options work similary.

Please let me know how to fix it?

  • What's the code for loading the images and populating the `image` array? Do you actually make multiple http calls? My guess is that the issue isn't with how the images are displayed but how they're retrieved from the server – andyk Nov 21 '21 at 01:13
  • Hello @andyk, I've just updated the question and have added HTML and PHP. Please let me know if you have any ideas. Thanks in advance. – iamatotalynewuser Nov 21 '21 at 01:47
  • just to confirm: you know lazy image loading is [part of html](https://caniuse.com/?search=lazy), right? You just need `loading="lazy"` in your `` tags. As is the [IntersectionObserver](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API), which you cam use to remove the loading attribute "if the user's scroll position is close enough". – Mike 'Pomax' Kamermans Nov 21 '21 at 01:54
  • Hello @Mike'Pomax'Kamermans, thank you for your comment. Yes, but 1. In this case I lazy load not only images from `` but also whole divs triggered by images loading. Divs are images and `

    text

    ` above the images. 2. Not for this topic, but I don't know why, `loading="lazy"` doen't always work. I don't have a clue why. 3. At the moment I don't know how to retrieve by PHP all images from ``. I don't want to enter `loading="lazy"` manyally in WP panel every time when I make a new post.
    – iamatotalynewuser Nov 21 '21 at 02:20

0 Answers0