set background javascript code example
Example 1: javascript style background color
function changebackground(){
document.getElementById('id').style.backgroundColor = 'green' ;
}
function changebackground(){
document.body.style.backgroundColor = 'green';
}
Example 2: js background
document.body.style.background = "#f3f3f3 url('img_tree.png') no-repeat right top";
Example 3: set an attribute background image javascript
document.body.style.backgroundImage = "url('img_tree.png')";
Example 4: document.body.style.background
document.body.style.backgroundColor = "red";
Example 5: set an attribute background image javascript
function bg() {
var imgCount = 3;
var dir = 'http://local.statamic.com/_themes/img/';
var randomCount = (Math.floor(Math.random() * imgCount));
var images = ['001.png', '002.png', '003.png'];
document.getElementById('banner').setAttribute("style", "background-image: url(" + dir + images[randomCount] + ");background-repeat: no-repeat;background-size: 388px 388px");
}
bg();