desandro / imagesloaded

:camera: JavaScript is all like "You images done yet or what?"

Home Page:https://imagesloaded.desandro.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Run each function after each image is loaded instead of waiting for every image to load and run all functions at once

patrulea opened this issue · comments

imagesLoaded waits until all product’s images are loaded to run productRandomPosition on every product at once.

Though, I’d like to run productRandomPosition on every product as soon as its images load, independent of the rest of the images’ progress.

This is my current code:

products.forEach((product) => {
  // randomize product position when images are loaded
  imagesLoaded(product, () => {
    productRandomPosition(product);
  });
});

I know this is due to me being a JavaScript novice but I’d be appreciative of any advice.

I achieved this with no use of the library.

productImages.forEach((image) => {
  if (image.complete) {
    randomPosition(image.parentElement);
  } else {
    image.addEventListener("load", () => {
      randomPosition(image.parentElement);
    });
  }
});