gpt4 book ai didi

javascript - 更改div的背景图片url

转载 作者:行者123 更新时间:2023-11-28 18:36:39 25 4
gpt4 key购买 nike

我有一个 div,我想通过更改 css 背景图像来在背景中显示多个图像作为淡入淡出,但我无法找到最佳方法。

JSFiddle

我的 HTML:

<div id="background"></div>

Jquery

var bgArr = ["http://malsup.github.io/images/p1.jpg", "http://malsup.github.io/images/p1.jpg", "images/TopImage-03.jpg"];
var i = 0;

// Start the slide show
var interval = self.setInterval("swapBkgnd()", 5000)

function swapBkgnd() {
if (i > (bgArr.length - 1)) {
i = 0
$("#background").css("background-image", "url(" + bgArr[i] + ")");
} else {
$("#background").css("background-image", "url(" + bgArr[i] + ")");
}
i++;
};

CSS:

#background {
position: absolute;
min-height: 100%;
width: 100%;
height: auto;
top: 0;
left: 0;
background: url(http://2.bp.blogspot.com/-ayEwJpMGTPQ/USqliwPWo1I/AAAAAAAAHtI/ab6NHVy0Q48/s1600/tree.jpg) no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

最佳答案

用途:

Jsfiddle:http://jsfiddle.net/ook3ah4p/

var interval = self.setInterval(swapBkgnd, 5000) // true syntax

而不是:

var interval = self.setInterval("swapBkgnd()", 5000) // wrong syntax

对于动画:

$('#background')
.animate({opacity: 0}, 'slow', function() {
$(this)
.css({'background-image': "url(" + bgArr[i] + ")"})
.animate({opacity: 1});
});

关于javascript - 更改div的背景图片url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36953293/

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