Javascript 使用jquery自动更改背景图像
我正在使用以下jquery代码:Javascript 使用jquery自动更改背景图像,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在使用以下jquery代码: (function() { var curImgId = 0; var numberOfImages = 42; // Change this to the number of background images window.setInterval(function() { $('body').css('background-image','url(/background' + curImgId + '.jpg)');
(function() {
var curImgId = 0;
var numberOfImages = 42; // Change this to the number of background images
window.setInterval(function() {
$('body').css('background-image','url(/background' + curImgId + '.jpg)');
curImgId = (curImgId + 1) % numberOfImages;
}, 15 * 1000);
})();
这个CSS:
.body-1{
background: url("../background/Living Room.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
width: 100%;
height:100%;
}
在我的背景文件夹中,我有两个不同名称的图像。当我加载页面时,CSS代码中引用的图像会被加载,但是当使用jquery加载另一个页面时,我会一直得到白色背景
有人能帮我解决这件事吗?在word
background
之后缺少/
,图像文件名中有空格。dey实际上是不允许的,它将根据浏览器/doctype/服务器的不同而中断。在css中,您有。/background/客厅.jpg
,而在jQuery中,您没有在路径前提供两个点。这两个点用于在路径树的一个层次上上升。在jQuery:url(../background/'+curImgId+'.jpg)
中尝试这样写。正如Vivek所说,CSS路径中有一个空格。