据我所知,没有额外的标记是无法改变具有淡入淡出效果的背景图像的。你没有提供任何代码,所以我做了一个综合示例。
标记:
<div class="container">
<div class="bg">
</div>
<div class="content">
...
</div>
</div>
...
CSS:
/*
this is your main container
*/
.container {
width:300px;
height:300px;
}
/*
this one is needed to provide
independent background, when it fades
your content will still be available for seeing
*/
.bg {
width:300px;
height:300px;
background-color:yellow;
z-index:0;
}
/*
your content, obviously :)
*/
.content {
position:relative;
top:-300px;
z-index:1;
padding:5px; /*just for better looking*/
}
还有一些 jquery:
//in your case, here must be URLs for images
var arr = ["yellow", "blue", "green", "red"];
var i = 0;
var intervalID = setInterval(function() {
//when bg fades, we don't want to see
//transparent background
$('.container').css('background-color', arr[i]);
//make transparent, change background, make visible again
$('.bg').animate({opacity: 0}, 'slow', function() {
$(this).css({'background-color': arr[i]}).animate({opacity: 1});
});
//this can be random
i++;
if (i >= arr.length)
i = 0;
}, 2000); //change 2000 if you want different time between changes
//this one is illustating, how to stop background loop
//with "clearInterval". You might want to use it on :hover or smth like that
setTimeout(function(){
clearInterval(intervalID);
}, 20000);
这不是很灵活的解决方案,因为您需要知道元素的确切大小,但我认为您不会使用不同大小的图像作为背景。
附言工作示例 - http://jsfiddle.net/7xVAu/115/
希望这会有所帮助。