gpt4 book ai didi

Jquery,用计时器改变背景图像

转载 作者:行者123 更新时间:2023-11-30 23:47:17 24 4
gpt4 key购买 nike

我有两个用于该网站的背景图像,我希望它们每 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]);
});

关于Jquery,用计时器改变背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22075468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com