3

I have the following div element and within it I load images. I only want to show the div#main-slider when all the images have loaded within it:

I have tried the following jQuery:

$(window).load(function() {  
    $('#main-slider').animate({'opacity':1}, 300);
});

But it shows the div even when all the images aren't loaded.

CSS

#main-slider,
#main-slider #bo,
#main-slider #bg,
#main-slider #hugo{
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    -moz-opacity:0;
    -khtml-opacity: 0;
    opacity: 0;
}

HTML

<div id="main-slider" class="flexslider">
    <div id="boss" class="tab-container">
        <ul class="slides boss-slides clearfix">
            <li id="1">
                <img src="mobilelp/img/boss_mobile_1.png" />
            </li>
            <li id="2">
                <img src="mobilelp/img/boss_mobile_2.png" />
            </li>
            <li id="3">
                <img src="mobilelp/img/boss_mobile_3.png" />
            </li>
        </ul>
    </div>
    <div id="bo" class="tab-container">
        <ul class="slides bo-slides clearfix">
            <li id="1">
                <img src="mobilelp/img/bo_mobile_1.png" />
            </li>
            <li id="2">
                <img src="mobilelp/img/bo_mobile_2.png" />
            </li>
            <li id="3">
                <img src="mobilelp/img/bo_mobile_3.png" />
            </li>
        </ul>
    </div>
    <div id="bg" class="tab-container">
        <ul class="slides bg-slides clearfix">
            <li id="1">
                <img src="mobilelp/img/bg_mobile_1.png" />
            </li>
            <li id="2">
                <img src="mobilelp/img/bg_mobile_2.png" />
            </li>
            <li id="3">
                <img src="mobilelp/img/bg_mobile_3.png" />
            </li>
        </ul>
    </div>
    <div id="hugo" class="tab-container">
        <ul class="slides hugo-slides clearfix">
            <li id="1">
                <img src="mobilelp/img/hugo_mobile_1.png" />
            </li>
            <li id="2">
                <img src="mobilelp/img/hugo_mobile_2.png" />
            </li>
            <li id="3">
                <img src="mobilelp/img/hugo_mobile_3.png" />
            </li>
        </ul>
    </div>
    <ul class="flex-direction-nav">
        <li><a href="#" class="flex-prev">Previous</a>
        </li>
        <li><a href="#" class="flex-next">Next</a>
        </li>
    </ul>
</div>
4

3 回答 3

2

我也有这个要求。我发现使用imagesLoaded 插件成功了。

基于我目前在我的应用程序中使用的伪代码:

$('#main-slider').imagesLoaded({
    progress: function (isBroken, $allImages, $loadedImages, $brokenImages) {
        console.log('ok: ' + $loadedImages.length + ', bad: ' + $brokenImages.length);
    },
    done: function ($images) {
        $('#main-slider').animate({'opacity':1}, 300);
    }
});
于 2013-08-22T18:53:48.993 回答
1

它可以变得非常简单(小提琴):

$(function () {
    var $img = $(".waitFor img"),
        n = $img.length;
    $img.on("load error", function () {
        !--n && $(".waitFor").fadeIn(300);
    });
});

这有效地等待所有图像在整个容器中消失之前加载或出错。

于 2013-08-22T19:18:53.590 回答
0

这是一个足够简单的问题,我不想为此使用插件。这实际上很简单,因为 jQuery 为所有图像元素提供了一个方便的 .load() 函数:

$(function(){
    var loaded = 0;
    var total = 0;
    $(".waitFor img").each(function(i, e){
        total++;
        $(e).load(function(){
            loaded++;

            if(loaded == total)
                console.log("Everything loaded");
        });
    });
});

提琴手

于 2013-08-22T19:05:08.877 回答