-
Notifications
You must be signed in to change notification settings - Fork 441
Description
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.