imagesloaded icon indicating copy to clipboard operation
imagesloaded copied to clipboard

Does imagesLOaded not fire when the src attribute of an img element is changed with javascript?

Open k-risc opened this issue 11 years ago • 6 comments

I am using imagesloaded.pkgd.min.js and use the following javascript:

    …
$('#payable-preview a .overlay').show();
$('#payable-preview a img')
    .attr('src', …);
$('#payable-preview a img').imagesLoaded()
    .always(function() {
        $('#payable-preview a .overlay').hide();
    });

But the function defined in "always()" is triggered immediately each time this code is executed. Thus the overlay is never visible because it gets hidden right after it was shown.

k-risc avatar Mar 11 '14 15:03 k-risc

maybe try wrapping the imagesLoaded call with a setTimeout with like 10ms?

yairEO avatar Apr 23 '14 18:04 yairEO

I'm having this issue as well but in my situation I am completely replacing an img tag with a new one containing a different src image using jquerys replaceWith(). Am using imagesLoaded after the replaceWith() call to fire a new "fade-in" effect once the image is loaded. It seems to occur randomly but sometimes imagesLoaded seems to just instantly fire its complete/always message, leading to me getting an incorrect height calculation (0px) on the container w/ the new image.

is the setTimeout the way to go?

EDIT: I wrapped a setTimeout with a 100ms delay around my imagesLoaded call and everything seems to be working now. Hrmph?

saumets avatar Apr 29 '14 13:04 saumets

@skalfyfan do you have a live demo for this bug (a jsbin or jsfiddle) ?

0xadri avatar Nov 20 '14 08:11 0xadri

Actually imagesloaded fails rarely, but the reason is unknown. Sometimes I use setTimeout as a failsafe but it's an ugly hack.

yairEO avatar Nov 20 '14 09:11 yairEO

@k-risc The answer to your question, in short: Yes, imagesLoaded does not fire when the src attribute of an img element is changed with javascript, this happens with Firefox. see https://github.com/desandro/imagesloaded/issues/121

Until the workaround mentioned in this issue is fully tested, you're probably "safer" by using imagesLoaded only on newly added images element.

0xadri avatar Nov 20 '14 09:11 0xadri

@yairEO you more details regarding this on https://github.com/desandro/imagesloaded/issues/121

0xadri avatar Nov 20 '14 09:11 0xadri