1

有没有办法等到所有背景图像都加载完毕后再执行某些代码?我知道这适用于一张图片,但我怎样才能适应各种图片呢?

var firstBackgroundImage = new Image();         
                firstBackgroundImage.onload = function () {
                        $('#main-slider').animate({opacity:1}),600;

            }; 
            firstBackgroundImage.src = "http://www.domain.com/img/iphone.png";

HTML

 <div id="main-slider"></div>

CSS

#main-slider{
   opacity:0;
}

谢谢!

4

2 回答 2

1

尝试:

$(window).load(function() {
    // this code gets executed when all content of the page (including images) is loaded 
});
于 2013-08-26T09:12:30.577 回答
1

如果你想要它完整的页面。我的意思是加载图像将显示隐藏您的主 div,直到每个组件都被完全加载..

 $(document).ready(function () {
            // calculate height
            var screen_ht = jQuery(window).height();
            var preloader_ht = 5;
            var padding = (screen_ht / 5) - preloader_ht;
            jQuery("#preloader").css("padding-top", padding + "px");


            // loading animation using script 

            function anim() {
                jQuery("#preloader_image").animate({ left: '1px' }, 2000,
                function () {
                    jQuery("#preloader_image"), animate({ left: '1px' }, 2000);
                }
                );
            }
            //anim();
        });
    function hide_preloader() {
    // To apply Fade Out Effect to the Preloader 
    jQuery("#preloader").fadeOut(1000);
    }
    </script>
<style>
    #preloader {background: #1c1c1c;position:fixed;left:0px; top:0px; width:100%; height:100%; text-align:center;color:#fff;z-index: 100000000;}
    #preloader div {width:228px;height:240px;margin:auto;padding:10px 0;text-align:center;overflow:hidden;}
    #preloader_image {width:228px;height:240px;position: relative;left:0px;top:-10px;}
</style>
</head>

<body id="home" onload="hide_preloader()">
    <div id="preloader">
        <div>
            <div id="preloader_image">
                <img src="loading.gif" style="position: absolute;bottom: 0;left: 35%;">
            </div>
        </div>
    </div>
    </body>
于 2013-08-26T09:13:12.940 回答