我有两个用于该网站的背景图片,我希望它们每5秒自动更改一次。有人可以看看我的jQuery代码并告诉我我做错了吗?
$(function() {
var body = $(‘body’);
var backgrounds = new Array(
‘url(images/hso-palmtree-background.jpg)’,
‘url(images/hso-boardwalk-background.jpg)’
);
var current = 0;
function nextBackground() {
body.css(
‘background’,
backgrounds[current = ++current % backgrounds.length]
);
setTimeout(nextBackground, 5000);
}
setTimeout(nextBackground, 5000);
body.css(‘background’, backgrounds[0]);
});
您的代码是正确的,您只需要更改反引号即可。将‘
更改为'
。
这里是已清除的修订:http://jsfiddle.net/X2NqX/
$(function () {
var body = $('body');
var backgrounds = [
'url(http://static.jsbin.com/images/jsbin_static.png)',
'url(http://static.jsbin.com/images/popout.png)'];
var current = 0;
function nextBackground() {
body.css(
'background',
backgrounds[current = ++current % backgrounds.length]);
setTimeout(nextBackground, 5000);
}
setTimeout(nextBackground, 5000);
body.css('background', backgrounds[0]);
});