2015-12-31 82 views
1

我正在隨機顯示一幅完整的背景圖像,但我無法控制。隨機背景圖像以JavaScript啓動

var images = [], 
index = 0; 

images[0] = "img/y0.gif"; 
images[1] = "img/y1.gif"; 
images[2] = "img/y2.gif"; 
images[3] = "img/y3.gif"; 
images[4] = "img/y4.gif"; 
images[5] = "img/y5.gif"; 

index = Math.floor(Math.random() * images.length); 
document.body.style.background = "url('+images[index]+') no-repeat center center fixed"; 

以空白屏幕。

我的CSS:

@import url(http://fonts.googleapis.com/css?family=Advent+Pro:100); 
html { 
    margin: 0; 
    padding: 0; 
    width:100%; 
    height:100%; 
} 
body { 
    background-size: cover; 
    height: 100%; 
} 

可以什麼原因呢?

回答

0
document.body.style.background = "#fff url('"+images[index]+"') no-repeat center center fixed"; 

現在工作..

+0

如果您的答案對您有用,並且您找到答案,您可能需要在下面標記並接受接受的答案:) – TrojanMorse

+0

我無法在兩天內接受我自己的答案。 :) – sakarya

4
"url('+images[index]+') no-repeat center center fixed"; 

應該

"url('"+images[index]+"') no-repeat center center fixed"; 
+0

沒有。不工作。現在Firefox得到TypeError:document.body爲null錯誤。 – sakarya

+0

這與我提出的建議無關... – Andy