我的图像文件夹中有5个jpg图像.我想要一个div来自动逐个改变背景.如何使用jQuery做到这一点?
我可以使用jQuery操作一次background属性,但是当我尝试自动更改和循环时,我被卡住了.
$( document ).ready(function() {
$(".topstrip").css('background-image', 'url("/static/img/website/banner/factory_plane-normal.jpg")')
});
Run Code Online (Sandbox Code Playgroud)
Use setInterval function. Sample Code.
$(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);
});
Run Code Online (Sandbox Code Playgroud)