Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div element when all images within it have loaded

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>
like image 721
user1937021 Avatar asked Jan 30 '26 10:01

user1937021


1 Answers

I also had this requirement. I found success using the imagesLoaded plugin.

Pseudo code based on what I currently use in my application:

$('#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);
    }
});
like image 80
ryan Avatar answered Feb 02 '26 01:02

ryan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!