【发布时间】:2016-01-29 16:21:35
【问题描述】:
我需要遍历css background-image
有人知道为什么这里没有改变背景吗:http://codepen.io/anon/pen/dGKYaJ
var bg = $('.background').css('background-image');
bg = bg.replace('url(','').replace(')','');
var currentBackground = 0;
var backgrounds = [];
backgrounds[0] = bg;
backgrounds[1] = 'https://placehold.it/300x300&text=1';
backgrounds[2] = 'https://placehold.it/300x300&text=2';
function changeBackground() {
currentBackground++;
if(currentBackground > 2) currentBackground = 0;
$('.background').fadeOut(3000,function() {
$('.background').css({
'background-image' : "url('" + backgrounds[currentBackground] + "')"
});
$('.background').fadeIn(3000);
});
setTimeout(changeBackground, 7000);
}
$(document).ready(function() {
setTimeout(changeBackground, 7000);
});
欢迎提出任何建议
拉
【问题讨论】:
-
您确定您不是在寻找
setInterval吗? developer.mozilla.org/en-US/docs/Web/API/WindowTimers/…
标签: javascript css loops