Skip to content

Check if image is loaded? #49

@aderaaij

Description

@aderaaij

It seems that in a gallery with a large numbers of items it can happen that data-loaded=true or any class you add in the callback, for that matters, can be applied to an image while the background image is not yet fully loaded. This results in a suddenly appearing image (especially when the images itself are fairly large) because the transition was already applied before the image was loaded in the browser.

I would propose to test if the image is actually loaded before adding the data-loaded=true attribute. Maybe like

const imgLoad = new Image();
imgLoad.onload = () => {
  element.dataset.loaded = true;
}
imgLoad.src = element.dataset.src;

There might be better ways to check if the images are loaded, but you catch my drift :)

edit: This does lead to the loading of two files when the srcset file which is loaded is different from the src file. This could be fixed by polling if element.currentSrc is set and use that image for the imgLoad.src.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions