Saturday, November 7, 2015

Dynamically change background color randomly

Hexadecimal color values are supported in all major browsers.

A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal integers specify the components of the color. All values must be between 00 and FF.

For example, the #0000FF value is rendered as blue, because the blue component is set to its highest value (FF) and the others are set to the lowest value (00).

Below is the script how to get randomly color.

Javascript Version

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var color = "0123456789ABCEDF";

function changeColor() {
    var temp = "";
    for (var a = 0; a < 6; a++)
        temp += color[Math.floor(Math.random() * color.length)];


    document.body.style.backgroundColor = "#" + temp;
}

function start() {
    setInterval("changeColor()", 1000)
}

start();

No comments:

Post a Comment