有没有办法在执行某些代码之前等到所有背景图像都已加载?我知道这适用于一个图像,但我如何适应各种图像呢?
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;
}
谢谢!
答案 0 :(得分:1)
尝试:
$(window).load(function() {
// this code gets executed when all content of the page (including images) is loaded
});
答案 1 :(得分: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>