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.

Fading Links Using jQuery: dwFadingLinks

28 Responses »

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
	});
});

Discussion

  1. October 9, 2008 @ 10:53 am

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

  2. October 9, 2008 @ 10:59 am

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

  3. October 9, 2008 @ 12:46 pm

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

  4. October 9, 2008 @ 12:47 pm

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

  5. bob
    October 9, 2008 @ 1:03 pm

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

  6. October 10, 2008 @ 9:41 am

    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.

  7. alex
    October 12, 2008 @ 7:01 am

    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

  8. October 12, 2008 @ 2:14 pm

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

  9. ramiken
    October 20, 2008 @ 6:38 am

    Sexy script!

  10. October 30, 2008 @ 4:15 pm

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

  11. November 11, 2008 @ 5:46 pm

    nice and simple

  12. stevelucky
    December 27, 2008 @ 5:06 pm

    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.

  13. scott
    March 18, 2009 @ 8:51 pm

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

  14. missy
    April 12, 2009 @ 2:23 pm

    Doesn’t work in Chrome :(

  15. jesse skeens
    April 12, 2009 @ 2:37 pm

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

  16. April 26, 2009 @ 6:54 pm

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

  17. paolo
    May 8, 2009 @ 5:09 am

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

  18. July 5, 2009 @ 8:38 pm

    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?

  19. July 14, 2009 @ 1:51 am

    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 :)

  20. tony
    August 17, 2009 @ 5:03 am

    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>

  21. November 10, 2009 @ 1:30 pm

    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!

  22. danny
    November 19, 2009 @ 6:44 am

    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

  23. December 4, 2009 @ 11:49 am

    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.

  24. daniel
    December 22, 2009 @ 8:16 pm

    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.

  25. March 25, 2010 @ 7:04 am

    I’m kinda dumb, PLS do tell how the heck do I implement this ? where do I paste it how…..
    I’m loading the jQuery from google.
    Thanks so much !!!

  26. May 1, 2010 @ 9:23 am
  27. marius
    June 10, 2010 @ 10:20 am

    How can i integrate this in phpbb3 ?

  28. nick
    June 18, 2010 @ 9:42 pm

    This is a nice script dave, but there are a few problems. If a hover state is defined in CSS the transition doesn’t work initially. If you don’t have a hover state defined and someone has JavaScript disabled then there is no hover state at all. This is problematic.

    Any work-around for a gracefully degrading version of this? It would also be nice if it just faded the CSS. Rather than defining it in the JS itself.

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!