我有这样的样式ID
#Myimage {
width: 797px;
height: 317px;
background:url(images/templatemo_header_pizza.jpg) no-repeat;
我想每 5 秒更换一次图片。
我是 JavaScript 和 jQuery 的新手,但主要想法是这样的
function ()
{
ImageArray = LoadImage("Baseimage/*.jpg")
while(true)
{
For(i in ImageArray)
{
Myimage.background:url = i;
waitfor(5);
}
}
}
最佳答案
var imgArray = ["image1.jpg","image2.jpg"];
var counter = 0;
function changeImages() {
counter++;
$("#MyImage").css("background-image",'url(' +imgArray[counter] + ')')
if(counter == 1) {
counter = -1;
}
}
setInterval(changeImages,5000)
关于javascript - 动态改变CSS样式中的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227824/