用JQuery选择随机背景

时间:2015-04-04 11:15:10

标签: jquery html css

我的代码没有返回错误,但只留下叠加效果而没有背景图片:

$(function() {
    var totalCount = 8;
    var bg = Math.ceil(Math.random() * totalCount);

    $("html").css({
        "background": "linear-gradient(to bottom, rgba(0, 0, 0, 0.4) 0%, rgba(0, 0, 0, .8) 100%), url(../img/bg0"+bg.toString()+".png) no-repeat center center fixed"
    });
}); 

2 个答案:

答案 0 :(得分:1)

 $("html").css({"background": "linear-gradient(to bottom, rgba(0, 0, 0, 0.4) 0%, rgba(0, 0, 0, .8) 100%), url('../img/bg0"+bg.toString()+".png') no-repeat center center fixed"});

你应该有url('../img/bg0"+bg.toString()+".png'),即网址内的路径应该是单引号

答案 1 :(得分:0)

你有语法错误 - 忘记“'”

$(function() {
    var totalCount = 8;
    var bg = Math.ceil(Math.random() * totalCount);
    $("html").css({"background": 
             "linear-gradient(to bottom, rgba(0, 0, 0, 0.4) 0%, 
              rgba(0, 0, 0, .8) 100%), 
              url('../img/bg0"+bg.toString()+".png') 
              no-repeat center center fixed"});
});