仅在完全加载后显示所有图像

suj*_*jal 12 jquery

<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>
Run Code Online (Sandbox Code Playgroud)

我想显示所有id ="all-images"的图像,只有当这个id里面的所有图像都完全加载时(Jquery).

在加载所有图像之前,此部分显示"正在加载..."文本

kar*_*m79 20

假设你的div被预先隐藏:

$("#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");
});
Run Code Online (Sandbox Code Playgroud)