jQuery Random Link Color Animations

By  on  

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() {
	var randomLinks = $('a.random-color');
	var original = randomLinks.css('color');
	randomLinks.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.

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    MooTools Zebra Table Plugin

    I released my first MooTools class over a year ago. It was a really minimalistic approach to zebra tables and a great first class to write. I took some time to update and improve the class. The XHTML You may have as many tables as...

  • By
    Use Custom Missing Image Graphics Using MooTools

    Missing images on your website can make you or your business look completely amateur. Unfortunately sometimes an image gets deleted or corrupted without your knowledge. You'd agree with me that IE's default "red x" icon looks awful, so why not use your own missing image graphic? The MooTools JavaScript Note that...

Discussion

  1. 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. Nice idea David!

    The animation is a little slow though…

  3. 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. @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. Hi David.

    Its is simply superb.

  6. John Smith

    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. @John Smith: Works for me!

  8. John Smith

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

  9. tuft

    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. 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

    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. 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?

  13. Hardy

    Hi there,
    unfortunately it does not work in combination with a slide out panel (based on jQ) in FF and Chrome. Does anybody know about that and is there anything I can do to make it work?????
    Thanks in advance.

    Hardy
    Berlin, Germany

  14. Victor Stroe

    nice code man! thx for sharing

    @theorie

    you can use this:

    colors =
    [
    “#37C78F”,
    “#C9DE55”,
    “#FF4D38”,
    “#CC2249”,
    “#380C2A”
    ]
    var col = colors[Math.floor(Math.random()*colors.length)];

  15. naty

    hi, i want to create a random spinning roulette or maybe something like a slot machine code, sp that the user will press “go” and the different options (which i’ll enter) will begin to spin and then stop on a random one. any help ?

  16. Nice Work. But If we hover it fast over all the links. It is not working in the way that it pretend to be.

  17. Thank you.. Great stuff..

  18. Nice Algorithm! I will use it on our page.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!