我的图像文件夹中有 5 张 jpg 图像。我想要一个div自动一一改变背景。如何使用 jQuery 做到这一点?
我可以使用 jQuery 操作一次背景属性,但当我尝试自动更改和循环时,我陷入困境。
$( document ).ready(function() {
$(".topstrip").css('background-image', 'url("/static/img/website/banner/factory_plane-normal.jpg")')
});
最佳答案
使用setInterval函数。示例代码。
$(document).ready(function () {
var imageFile = ["Image.jpg", "Image1.jpg", "Image2.jpg", "Image4.jpg"];
var currentIndex = 0;
setInterval(function () {
if (currentIndex == imageFile.length) {
currentIndex = 0;
}
$(".topstrip").css('background-image', 'url("/static/img/website/banner/' + imageFile[currentIndex++] + '")');
}, 3000);
});
关于jquery - 如何使用jquery连续改变div的背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277030/