To create a random number in a range, we need to do something like
minValue + random Number * (maxValue - minValue)
ie, if you want to create a random number between the range of 100 to 200 , we must do
var rand = 100 + Math.floor(Math.random() * (200 - 100));
which gives a random number between the range of 100-200
using this basic rule, we can generate random color from a given range
var range = [{0 : 150,1 : 200}, {0 : 200,1 : 230},{0 : 10,1 : 20}]; function rgb() { var color ='rgb('; for(var i = 0; i< 3; i++) { color += rand(range[i]['0'], range[i]['1']) + ','; } return color.replace(/\,$/,')') } function rand(min, max) { return min + Math.floor(Math.random() * (max - min)); } alert(rgb());
Try this code http://jsbin.com/tuday
EDIT :
$(function() { var cache = $('#hover').css('background-color'); $('#hover').hover(function() { $(this).css({'background-color' : rgb() }); },function() { $(this).css({'background-color' : cache }); }); });
Example : http://jsbin.com/iwovew
user372551
source share