Skip to content

Image loaded event called not when image trully loaded on firefox #342

Closed
@jegbagus

Description

@jegbagus

i use data srcset without src on image tag

<img width="120" height="86" src="" size-120x86 lazyload" alt="" data-full-width="1920" data-full-height="1272" data-src="http://localhost/photo-120x86.jpg" data-sizes="auto" data-srcset="http://localhost/photo-120x86.jpg 120w, http://localhost/photo-260x186.jpg 260w, http://localhost/photo-350x250.jpg 350w" data-expand="-20" />

but loaded class added too early (not when image loaded, but when image get the view). its working fine on chrome & microsoft edge. but not on firefox.

Activity

aFarkas

aFarkas commented on Jan 6, 2017

@aFarkas
Owner

I would need a simplified testcase on codepen, jsfiddler, jsbin or what you want.

jegbagus

jegbagus commented on Jan 6, 2017

@jegbagus
Author

hi aFarkas, thank you for replying.

Please take a look right here. https://jsfiddle.net/432zy2en/
on google chrome, when you scroll down, animation is smooth because lazyloaded class added correctly after image loaded.

but if you try it on firefox, it will flashing. it may because lazyloaded class added before image loaded.

Thank you

jegbagus

jegbagus commented on Jan 7, 2017

@jegbagus
Author

hi aFarkas, i solve this issues by adding additional function when checking if image already loaded.

previously image directly checked with complete property attached to element. to fix this issues on firefox it will need additional check if image is truly loaded.

rAF(function(){
	if( !firesLoad || elem.complete ){
		if(firesLoad){
			resetPreloading(event);
		} else {
			isLoading--;
		}
		switchLoadingClass(event);
	}
}, true);

into this one :

rAF(function(){
    if( !firesLoad || IsImageOk(elem) ){
	if(firesLoad){
	    resetPreloading(event);
	} else {
	    isLoading--;
	}
	switchLoadingClass(event);
    }
}, true);

with this function i get from stackoverflow :

function IsImageOk(img) {
	// During the onload event, IE correctly identifies any images that
	// weren’t downloaded as not complete. Others should too. Gecko-based
	// browsers act like NS4 in that they report this incorrectly.
	if (!img.complete) {
	    return false;
	}

	// However, they do have two very useful properties: naturalWidth and
	// naturalHeight. These give the true size of the image. If it failed
	// to load, either of these should be zero.

	if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) {
	    return false;
	}

	// No other way of checking: assume it’s ok.
	return true;
}
added a commit that references this issue on Jan 14, 2017
1608a1b
aFarkas

aFarkas commented on Jan 14, 2017

@aFarkas
Owner

I found a better solution for this. FF does not synchronously update img.complete after src/srcset was changed to false, but it is updated in the task after. Due to the fact, that we already work with rAF here, it was easy to fix. I additionally guarded this using naturalWidth > 1, so this also works with a data uri image.

chpio

chpio commented on Mar 20, 2019

@chpio

this isn't fixed for me, the img isn't fully loaded after the lazyloaded event was fired.

i still need to workaround that by adding an onload listener:

// `this.element` is the parent container
this.element.addEventListener(
			'lazyloaded',
			event => {
				try {
					let elem = event.target;
					let img = elem.querySelector('img');
					img.addEventListener('load', () => onLazyLoadedImage(event));
				} catch (_) {
					// ignore...
				}
				onLazyLoadedImage(event);
			}
		);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Image loaded event called not when image trully loaded on firefox · Issue #342 · aFarkas/lazysizes