2015-04-04 59 views
0

我的代碼返回沒有錯誤,但只留下沒有背景圖片的疊加效應:選擇隨機背景與jQuery

$(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" 
    }); 
}); 
+0

我看你只使用backgr在CSS方法中的ound風格...爲什麼你需要paranthesis {..} ...?你可以嘗試沒有括號..我認爲該方法的值屬性不允許逗號.. – Lucky 2015-04-04 12:58:52

回答

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')即內部URL路徑應該是單引號

+0

此外,我的圖像目錄不需要「../」出於某種奇怪的原因。考慮到css文件位於根目錄下的路徑中。 – Axiom 2015-04-04 23:56:16

0

你有syntx的錯誤 - 忘記 「'」

$(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"}); 
});