13

In

<div id="all-images">
    <img src="images/1.jpg">
    <img src="images/2.jpg">
    <img src="images/3.jpg">
    <img src="images/4.jpg">
    <img src="images/5.jpg">
    <img src="images/6.jpg">
</div>

I want to show all Images of id="all-images" only when all the images inside this id will load completely (Jquery).

And before loading all images this section shows "Loading.." text

sujal
  • 1,058
  • 3
  • 18
  • 29

2 Answers2

21

Assuming your div is pre-hidden:

$("#loadingDiv").show();
var nImages = $("#all-images").length;
var loadCounter = 0;
//binds onload event listner to images
$("#all-images img").on("load", function() {
    loadCounter++;
    if(nImages == loadCounter) {
        $(this).parent().show();
        $("#loadingDiv").hide();
    }
}).each(function() {

    // attempt to defeat cases where load event does not fire
    // on cached images
    if(this.complete) $(this).trigger("load");
});
Minhaz
  • 937
  • 1
  • 10
  • 25
karim79
  • 339,989
  • 67
  • 413
  • 406
0

http://api.jquery.com/load-event/

Try attaching a load event handler to your "all-images" div. If the API is to be trusted, that event will be triggered when all the subimages are loaded.

So I would have some sort of document.onready handler which will hide your images and insert the "Loading..." text, then your load event handler will show your images and remove the loading text.

Hope this helps.

danbo
  • 246
  • 1
  • 3
  • 6