question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Check if image is loaded?

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
justinasdmtcommented, Feb 13, 2019

Hey guys, Any update on this? Does anyone have a solution to really check if the image is fully loaded?

2reactions
forepointmccommented, May 16, 2019

Just have to say that I don’t recommend this library to anyone. It sucks ass. I used it on two projects after reading the CSS Tricks article about it, and I’ve had nothing but issues with it. It doesn’t check to see if an image has loaded, data-loaded=true is always present on the element. Images sometimes never load, or half load. The fact that this core issue is still unresolved after so long says it all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check whether an image is loaded or not
Method 1: Using attributes of <img> to check whether an image is loaded or not. · Example: · Output: when image is successfully...
Read more >
Check whether an image is loaded with JavaScript
To determine whether an image has been completely loaded, you can use the HTMLImageElement interface's complete attribute. It returns true if the image...
Read more >
Check if an image is loaded (no errors) with jQuery
prototype.isLoaded = function() { // See if "naturalWidth" and "naturalHeight" properties are available. if ...
Read more >
How to detect when the images are loaded - JavaScript - iDiallo
It is simple to load images to the page using JavaScript. We can create it using two methods then append it to the...
Read more >
HTMLImageElement.complete - Web APIs - MDN Web Docs
The read-only HTMLImageElement interface's complete attribute is a Boolean value which indicates whether or not the image has completely loaded.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found