Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random color for animate in jquery

I trying to add the text effect using jquery with ui animate. Is there is a way to show all possible color randomly without defined all colors. For example color combination is based on RGB. Using the animate with color like

setInterval(function() {
jQuery(".font-style").animate({color: "red"}, 2000).
          animate({color: "green"}, 2000).animate({color: "blue"}, 2000);}, 400);

Is there is any possibilities to show the color combination of RGB Randomly in jquery. Any suggestion would be great.

Thanks.

like image 513
Vignesh Pichamani Avatar asked Dec 07 '22 05:12

Vignesh Pichamani


2 Answers

You can generate a random color like this:

var newColor = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
jQuery(".font-style").animate({color: newColor}, 2000); // animate

This will create a random hex color such as #ff00cc.

Note: regular jQuery doesn't animate colors, you'll have to use jQuery color plugin or jQuery UI

like image 165
Yotam Omer Avatar answered Dec 17 '22 05:12

Yotam Omer


You could do something like this:

var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';

And than put col in your animate:

jQuery(".font-style").animate({color: col}, 2000)
like image 30
putvande Avatar answered Dec 17 '22 04:12

putvande