Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

jQuery Random Link Color Animations

12 Responses »

We all know that we can set a link's :hover color, but what if we want to add a bit more dynamism and flair? jQuery allows you to not only animate to a specified color, but also allows you to animate to a random color.

The jQuery JavaScript

$(document).ready(function() {
	original = $('a.random-color').css('color');
	$('a.random-color').hover(function() { //mouseover
		var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
		$(this).animate({
			'color': col,
			'paddingLeft': '20px'
		},1000);
	},function() { //mouseout
		$(this).animate({
			'color': original,
			'paddingLeft': '0'
		},500);
	});
});

For the color animation portion, you'll need an extra Color Animations jQuery plugin. The padding nudge is another neat effect. This effect isn't for everyone, but it can add another subtle effect to your jQuery-enabled website.

Discussion

  1. December 9, 2008 @ 9:18 am

    Nicely done David! But I think you should do 256 since – i believe – (Math.floor(Math.random() * 255)) will never return 255, just 0- 254.

  2. December 9, 2008 @ 9:53 am

    Nice idea David!

    The animation is a little slow though…

  3. December 9, 2008 @ 10:41 am

    Cool idea. I think that there should be some constraints on the colours though as sometimes the link turned off-white.

    var col = ‘rgb(‘ + (Math.floor(Math.random() * 51)) + ‘,’ + (Math.floor(Math.random() * 51)) + ‘,’ + (Math.floor(Math.random() * 153)) + 51′)’;

    for example will always (I think) be a colour between #000033 and #3333CC

  4. December 9, 2008 @ 7:47 pm

    @Benjamin Sterling: Good point. Updated.

    @James: Speed can be adjusted, so no worries there.

    @Clinton Montague: This is a great consideration and one I looked at when developing. I decided to leave it out because it’s a “per site” issue.

  5. December 14, 2008 @ 3:39 am

    Hi David.

    Its is simply superb.

  6. john smith
    May 8, 2009 @ 11:37 pm

    Great effect David! Thanks for sharing it with us.

    Does anyone know of a way to make this compatible with IE 7 by chance?

  7. May 9, 2009 @ 7:19 am

    @John Smith: Works for me!

  8. john smith
    May 10, 2009 @ 8:28 pm

    Cheers David – figured out my issue – I just had a conflicting css definition that I somehow glazed over.

  9. tuft
    November 17, 2009 @ 2:31 pm

    hi, just suggestion: call stop() on element before starting new animation:

    $(this).stop().animate({ …

    it will eliminate dancing menu item after multiple mouseover/mouseout…

  10. March 31, 2010 @ 3:03 pm

    Hi David, sorry this is such a backward question but can you tell me how I can use this code with my html. I don’t mean within my document, but referencing, say, a js file on my site to get this effect to take place?

    Thanks a lot.

  11. theorie
    May 6, 2010 @ 4:28 pm

    hello,

    i’m wondering how it would be possible to limit the number of colors to a defined list. if i only want to have 7 set colors, how would i go about defining those 7 colors?

    thanks in advance!

  12. August 23, 2010 @ 5:27 pm

    Well, this is just great!

    I’m trying to figure out how to modify this so that every link on a page, when loaded, has a random background color. With a little padding on each link, it will look kind of lego-ish or block pixel-y or something, which would be rad. Any ideas?

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!