1

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:

@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?

0

2 Answers 2

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

should be

"url('"+images[index]+"') no-repeat center center fixed";
Sign up to request clarification or add additional context in comments.

1 Comment

no. not working. Now Firefox getting TypeError: document.body is null error.
0
document.body.style.background = "#fff url('"+images[index]+"') no-repeat center center fixed";

now works..

1 Comment

you might want to mark and accept the accepted answer below if it worked for you and got you to the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.