Skip to content Skip to sidebar Skip to footer

Change Pre-defined Body Background Image

I found this script to change the body's background image when you click a link. It works, great except the only problem is is that I cannot have a pre-defined bg image in the css

Solution 1:

Keep the background in the CSS, and use this code instead

var backImage = [
    "images/bg0.png",
    "images/bg1.png",
    "images/bg2.png",
    "images/bg3.png",
    "images/bg4.png",
    "images/bg5.png"
];

function changeBGImage(whichImage) {
    if (document.body){
        document.body.style.backgroundImage = 'url('+backImage[whichImage]+')';
    }
}

Solution 2:

To change the background if you have one defined in the css you need to alter: document.body.style.backgroundImage attribute


Post a Comment for "Change Pre-defined Body Background Image"