gpt4 book ai didi

jquery - 在 JQuery 中循环浏览背景图像

转载 作者:行者123 更新时间:2023-12-01 08:22:05 24 4
gpt4 key购买 nike

基本上我需要的是一种每 5 秒更改页面背景图像并具有淡入淡出效果的简单方法。我遇到的问题是我想使用我的主体标签来执行此操作,并且我有这个 CSS:

body {
background: url("images/bg-home.jpg") no-repeat scroll center top #000000;
}

因此,循环需要将此 div 更改为 bg-home-1.jpg、bg-home-2.jpg 等...并且这必须是动态的,因为每个页面都有不同数量的图像,所以我 <强>不能使用这种方法:

  <div id="fading-images"> 
<img src="img/home-1.jpg">
<img src="img/home-2.jpg">
<img src="img/home-3.jpg">
</div>
setInterval(function(){
$('#fading-images:first-child').fadeOut('slow')
.next('img').fadeIn('slow')
.end().appendTo('#slideshow');},
4000);

}

希望有人能帮助我:)

最佳答案

fiddle :http://jsfiddle.net/G3Fyv/3/

html:

<html>
<body>
<div id="fader"></div>
<div id="background" class="img1"></div>
</body>
</html>

JavaScript:

$(document).ready(function() {

var $fader = $("#fader");
var $bg = $("#background");
var cur_img = 0;
var img_num = 5;

setInterval(function () {
var next_img = (cur_img === img_num - 1) ? 0 : cur_img + 1;
$fader.removeClass().addClass("img" + cur_img.toString()).css("display", "block").fadeOut(2000);
$bg.removeClass().addClass("img" + next_img.toString());
cur_img = next_img;
}, 5000);
});

CSS

.img1 { background-color: red; }
.img2 { background-color: green; }
.img3 { background-color: blue; }
.img4 { background-color: yellow; }
.img5 { background-color: grey; }

.bg_div {
position: absolute;
width: 100%;
height: 100%;
}

#background {
z-index: 1;
position: absolute;
width: 100%;
height: 100%;
}
#fader {
z-index: 2;
position: absolute;
width: 100%;
height: 100%;
}

关于jquery - 在 JQuery 中循环浏览背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6742418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com