【发布时间】:2016-12-16 09:45:54
【问题描述】:
我正在处理的网站上有一个块,其中包含一些按钮和背景图像。
我需要每 3 秒更改一次背景图片,所以我有以下 javascript:
jQuery('.myBlock').css({"background-image" : "url(img1)"});
var counter = 0;
function setBckImage(){
if(counter<3){
counter++;
} else {
counter=1;
}
switch (counter){
case 1:
jQuery('.myBlock').css({"background-image" : "url(img1)"});
break;
case 2:
jQuery('.myBlock').css({"background-image" : "url(img2)"});
break;
case 3:
jQuery('.myBlock').css({"background-image" : "url(img3)"});
break;
}
}
setInterval(setBckImage, 3000);
这可行,但变化是“突然的”,我希望它更平滑,添加淡入/淡出效果。我怎样才能做到这一点?我看到 this answer 导致 this link 但在此链接中,图像在 div 内,它们不是背景图像,所以我不明白如何使这个答案适应我的代码。
有没有办法做到这一点?
【问题讨论】:
标签: javascript jquery html css fading