简体   繁体   English

带有Javascript的随机背景图片

[英]Random Background Image with Javascript

I'm tring to show randomly a full background image, but I con't. 我想随机显示完整的背景图像,但我没有。

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";

Taking blank screen. 以空白屏幕。

My css: 我的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%;
}

What can the reason be? 原因可能是什么?

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

应该

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

现在可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM