我有一个向我的 div 添加背景图像的功能。但它没有显示。
这是我的功能
var totalCount = 6;
function changeBackground()
{
var num = Math.ceil( Math.random() * totalCount );
backgroundUrl = '/background/test2/'+num+'.jpg';
$('#background').css('background-image, url(' +backgroundUrl+ ')');
}
changeBackground();
每次刷新页面时,图像都会发生变化。我知道那部分正在工作,因为如果我改变
$('#background').css('background-image, url(' +backgroundUrl+ ')');
到
document.body.parentNode.style.backgroundImage = 'url(background/test2/'+num+'.jpg)';
它向我展示了我想要的图像。但是在html标签上。我想要它在 div 上,所以我可以用 jQuery 淡入它。
这是我的 CSS
#background {
width:100% !important;
height:100% !important;
background-repeat:no-repeat;
background-position:center center;
background-attachment:fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
我在控制台中没有收到任何错误,当我查找 css 时,css 中没有添加任何内容。我现在怎么能有什么问题?因此,将来我可以为自己解决问题所在。