I want to use ng-repeat
to show more then 100 images in a page. Those images are taking significant time in loading and i don't want to show them getting loaded to the users. So, I only want show them after all of them are loaded in the browser.
Is there a way to detect, if all the images are loaded?
you can use
load
event like this.Angular Directives
Solution for single image
HTML
JS
CSS
http://jsfiddle.net/jigardafda/rqkor67a/4/
if you see the jsfiddle demo, you will notice
src
image is only showing after image is fully loaded whereas in case ofsrc2
you can see image loading.(disable cache to see the difference)Solution for multiple images
HTML
JS
To detect if all images are loaded,
for each image i generated a
deferred
object and passed itsdeferred.resolve
as a image onload callback of the directive and then pushed thatdeferred
objectspromise
in an array. and after that i used$q.all
to detect if all those promise are yet resolved or not.http://jsfiddle.net/jigardafda/rqkor67a/5/
UPDATE: angular way added.
UPDATE: added solution for loading multiple images.
Check if all images are loaded
$('.wrap img').imagesLoaded(function(){ alert('all images loaded'); });
Note : this code worked for me, Source :
http://wowmotty.blogspot.in/2011/12/all-images-loaded-imagesloaded.html