Fading Links Using jQuery: dwFadingLinks

Written by David Walsh on Thursday, October 9, 2008


UPDATE: The jQuery website was down today which caused some issues with my example. I’ve made everything local and now the example works.

Earlier this week, I posted a MooTools script that faded links to and from a color during the mouseover and mouseout events. My thought was “why do a harsh a:hover color change when you can fade to that color?” Here’s how to implement link color fading using jQuery.

The jQuery Javascript – Plugin & Usage

/* plugin */
jQuery.fn.dwFadingLinks = function(settings) {
	settings = jQuery.extend({
		color: '#ff8c00',
		duration: 500
	}, settings);
	return this.each(function() {
		var original = $(this).css('color');
		$(this).mouseover(function() { $(this).animate({ color: settings.color },settings.duration); });
		$(this).mouseout(function() { $(this).animate({ color: original },settings.duration); });
	});
};

/* usage */
$(document).ready(function() {
	$('.fade').dwFadingLinks({
		color: '#008000',
		duration: 700
	});
});


Follow via RSS Epic Discussion

Commenter Avatar October 09 / #

I think this code has potential for abuse by the webmasters.

David Walsh October 09 / #
david says:

Hahahaha. No way — webmasters couldn’t use this because they use applets for links.

Commenter Avatar October 09 / #

Hey, this doesn’t work for me! I went to the example and after a LONG delay the page came up. the links I use Firefox 3.03. I just went back to it and it works now…. Is there some sort of delay about Jquery kicking in???

Commenter Avatar October 09 / #

Whoops, just refreshed this page and saw the note about the JQuery site being down… Isn’t that a possible reason not to use JQuery??

Commenter Avatar October 09 / #
bob says:

Awesome, how ever I would use $(this).stop().animate $(this).animate to make it a bit smother if you glide the mouse over.

Commenter Avatar October 10 / #
Mike says:

Could this be used to fade in a new background image too? Changing the color is nice, but having a background fade up would like much more striking.

Commenter Avatar October 12 / #
Alex says:

Thanks a lot david. I think It’s time to move my projects to jQuery now :)
Mootools is great, but jQuery is much easier for me.

Note: this doesn’ t mean that I’ll stop visiting your blog :D

Commenter Avatar October 12 / #
James says:

Hey Dave!

You haven’t mentioned the requirement for the UI effects core file which is needed in order for this to work at all. The jQuery core does not support color/backgroundColor animations by itself.

Also, instead of using the whole UI core file you can get away with just using the color portion of it (http://plugins.jquery.com/files/jquery.color.js.txt) which extends the functionality of the “animate” method in order to make it work with color.

@Dwight & Dave – If you’re not going to host jQuery yourself it’s best to link to it on GoogleCode: http://code.google.com/p/jqueryjs/

And I kinda agree with Mark in that this could easily be abused – I like the effect but I can imagine it getting heavily overused by a beginner…

Anyway, thanks for the tip Dave. :)

Commenter Avatar October 20 / #
ramiken says:

Sexy script!

Commenter Avatar October 30 / #

Nice script but couldn’t figure out why the color and duration was used twice. In the end I took out this portion:

settings = jQuery.extend({

color: ‘#ff8c00′,

duration: 500

}, settings);

and it still worked fine..

Commenter Avatar November 11 / #
Moksha says:

nice and simple

Commenter Avatar December 27 / #
stevelucky says:

how would i add this to my site for ALL links? i have several classes and want this to apply to any text link regardless of class.

Commenter Avatar March 18 / #
Scott says:

Very nice – I like this execution. Here’s a question: How could you keep the link you click to stay highlighted using this code?

Commenter Avatar April 12 / #
missy says:

Doesn’t work in Chrome :(

Commenter Avatar April 12 / #
Jesse Skeens says:

Not surprised, even simple radio buttons dont seem to work on Chrome.

Commenter Avatar April 26 / #
Rick says:

I really like this. However, when adding to a site I’m working on, as a progressive enhancement, I noticed a flaw: if a css :hover state is already declared for the links in question the fade-in only works on the second roll over (the initial roll over flicks to the hover state as it would normally). At least, it does in FFox and Opera – for once IE displays it correctly!!

Commenter Avatar May 08 / #
paolo says:

hi, beautiful script. what i have to edit, to change background-color instead color? thanks

Commenter Avatar July 05 / #

Yeah I notice the same problem that rick has: “if a css :hover state is already declared for the links in question the fade-in only works on the second roll over (the initial roll over flicks to the hover state as it would normally). At least, it does in FFox and Opera”

Does anybody know a fix for this?

Commenter Avatar July 14 / #
Olivia says:

I love this script! Its exactly what I was hoping to find. I have noticed a small glitch though that sometimes the hover colour sticks, and if I go down a long list of links, a bunch of them will randomly stay lit. It can only be cleared if I hover over it again. I’ve tried to fix it myself but I’m no JavaScript coder and it just wasn’t happening for me.

Anyway, thanks for making this script available :)

Commenter Avatar August 17 / #
Tony says:

How can I add a jQuery command to fade in the CSS hover effect from the following CSS A:Hover actions? I wanted it to fade in to the CSS A:Hover definition, reason being, where I will use the JS code, I will not know the CSS code before hand and it can change dynamically, so all I know is that the A href will have hover properties.

<style>
A{
Background: Red;
Display: Block;
Padding: 100px;
}

A:Hover{
Background: Blue;
}
</style>

<a href=”#”>Hover over me</a>

Commenter Avatar November 10 / #

Hi David,

I am a scripting novice. Got everything working well on my site, just wondering how I can keep the link from pulsing when I roll over a few links in a row?

Thanks!

Commenter Avatar November 19 / #
Danny says:

Hey David,

thanks for sharing this awesome simple code :D
It works just fine on my website but I still have a few questions:

1. As Scott mentioned before, is there a way to keep the link you clicked to stay highlighted?

2. Some of the hover links stay lit, is there a way to fix this?

Thanks
Danny

Commenter Avatar December 04 / #
Shrihari says:

Is there some way to make this work with normal css :hover definitions ? Or atleast to over-ride them ? Because, i don’t want my links to be without any hover effect when JS is disabled.

Commenter Avatar December 22 / #
daniel says:

For those who want the hover fade to stop part-way when the mouse leaves the link (before the fade has gone completely up), add this between the .mouseover(function) and the .mouseout(function):

$(this).mouseleave(function() { $(this).stop(settings.duration); });

So they should look like this together:

$(this).mouseover(function() { $(this).animate({ color: settings.color },settings.duration); });
$(this).mouseleave(function() { $(this).stop(settings.duration); });
$(this).mouseout(function() { $(this).animate({ color: original },settings.duration); });

That said, I’m a very novice scripter—this added line from me was little more than an educated guess.

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.