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.

MooTools Link Fading

22 Responses »

We all know that we can set a different link color (among other properties) on the hover event, but why not show a little bit more dynamism by making the original color fade to the next? Using MooTools 1.2, you can achieve that effect.

The MooTools JavaScript

window.addEvent('domready',function() {
	$each($$('.fade'), function(el) {
		var original = el.getStyle('color');
		var morph = new Fx.Morph(el,{ 'duration':'300', link:'cancel' });
		el.addEvents({
			'mouseenter' : function() { morph.start({ 'color':'#ff8c00' }) },
			'mouseleave' : function() { morph.start({ 'color': original }) }
		});
	});
});

For every link with the fade class, a transition occurs on the mouseenter and mouseleave events. You'll place the "to" color in the spot designated in comments in the code above.

What are your thoughts on this? Too much? Just right? Let me know!

Discussion

  1. October 6, 2008 @ 8:10 am

    Kinda cool, and could work in the right application. It’s always worth remembering what the users expects from a link though.

    Unfortunately events don’t finish if a new one is fired right now, so the links can become locked to their active colour until the mouse enters again.

  2. October 6, 2008 @ 8:20 am

    thats why i’m using mootools, thanks for the great idea, i’m looking to integrate it in my blog… ^^

  3. thomasd
    October 6, 2008 @ 8:52 am

    I agree with Paul, there’s a problem with effects starting and ending.
    Here is my solution, 100% mootools-sugar!

    $$(‘.fade’).set(‘morph’, {‘duration’: ’300′}).addEvents({
    ‘mouseenter’: function(){
    if(!this.retrieve(‘original’)){ this.store(‘original’, this.getStyle(‘color’));}
    this.morph({‘color’: ‘#ff8c00′});
    },
    ‘mouseleave’: function(){
    this.morph({‘color’: this.retrieve(‘original’)});
    }
    });

  4. October 6, 2008 @ 9:04 am

    @thomasd: I’ll try that sometime today! Thanks for contributing!

  5. norpius
    October 6, 2008 @ 9:06 am

    the problem can be resolved simply like this:

    var morph = new Fx.Morph(el,{ ‘duration’:’300′, link: ‘cancel’ });

  6. October 6, 2008 @ 9:17 am

    @norpius: Yep, just got that a few minutes ago.

  7. October 6, 2008 @ 10:00 am

    Hmm.. I thought Fx.Tween is enough for this..

  8. alex
    October 6, 2008 @ 1:08 pm

    Man, you’re F A N T A S T I C !

  9. October 6, 2008 @ 5:40 pm

    Why not use addClass()?

  10. October 7, 2008 @ 12:03 am

    How I can implement this on wordpress sidebar links…

    I’m got trouble adding class and id to the specified links.. tq..

  11. October 8, 2008 @ 4:06 pm

    I like this, useful and small. I think change my blog’s links. Thank you for sharing…

  12. speed
    October 9, 2008 @ 1:19 pm

    Hey one more cool script :)

    Thanks man!

    One quick question: How can I replace the “Color” with “Background Image” ?
    I tired but it ain’t working :(

    Any help would be appreciated!

  13. stefan
    October 24, 2008 @ 11:54 am

    yeah, I’m also looking at a way to do a background fade? any idea where to find this?
    cheers
    s

  14. cyrille
    November 7, 2008 @ 3:08 am

    Hi,

    Speed, Stefan, did you find the way to morph a photo by an other one ?
    I accept with pleasure your solutons if you got some !

    cheers

  15. h-a-r-v
    February 7, 2009 @ 11:48 pm

    Hey,

    What should I do to make it get the target color value from a specified css class instead of hard-coding it in the script as above?

    I had a special class written to do it in 1.11, but it doesn’t work under 1.2.1. I’ve already received some feedback and I guess I can fix it, but that person suggested me using fx.morph, but looking at the given example I can’t really figure out how.

    Thanks in advance,
    Kind regards,
    h-a-r-v

  16. February 20, 2009 @ 9:38 am

    Hello,

    I’m trying to write a link fading plugin for Joomla but i cant get it to work because Joomla uses Mootools 1.11
    It seems mootools 1.11 doesnt know $each and Fx.morph
    Do you have any advice how to get this to work with mootools 1.11?

    Thank you!

  17. johan
    February 20, 2009 @ 1:58 pm

    Looks very sweet & simple and will deff. try this out. :)
    But …
    I would like only the underlines to fade, not the letters is this possible?
    If so, how?

  18. February 20, 2009 @ 4:08 pm

    I think this should work:

    Set in the css: “text-decoration: none;” for .fade and “text-decoration: none; border-bottom: #000000 1px solid;” for .fade:hover

    Then replace in the script “color” with “border-bottom” and replace “#ff8c00″ with “#ff8c00 1px solid”

    Adjust the color-codes and youre done ;)

  19. birke
    March 17, 2009 @ 1:18 am

    Isn´t it possible to use the link fading with the email protection script? Both together doesn´t work. :(

  20. September 14, 2009 @ 4:51 am

    Hey, here’s code for MooTools 1.11 for menu in Joomla. Just assign ID in menu module configuration.
    I got help from http://demos111.mootools.net/Fx.Styles

    window.addEvent(‘domready’, function() {

    var list = $$(‘#mainmenu li a’);
    var original;
    list.each(function(element)
    {
    if ( element.parentNode != $(‘current’)) {
    var fx = new Fx.Styles(element, {duration: 1000, wait: false, transition: Fx.Transitions.Sine.easeIn});
    original = element.getStyle(‘color’);

    element.addEvent(‘mouseenter’, function(){
    fx.start({
    //’opacity’ : 1,
    ‘color’: ‘#333333′
    });
    });

    element.addEvent(‘mouseleave’, function(){
    fx.start({
    //’opacity’ : 0.8,
    ‘color’: original
    });
    });
    };
    });
    });

  21. December 19, 2009 @ 1:12 pm

    man you are awesome… really helpd me!!!

  22. paps
    February 2, 2010 @ 2:57 am

    hi david
    great code and thanks a lot for all you make !!

    i use your script in a website with “opacity” and it work !

    window.addEvent(‘domready’,function() {
    $each($$(‘.fade’), function(el) {
    var original = el.getStyle(‘opacity’);
    var morph = new Fx.Morph(el,{ ‘duration’:’100′, link:’cancel’});
    el.addEvents({
    ‘mouseenter’ : function() { morph.start({ ‘opacity’:0.5}) },
    ‘mouseleave’ : function() { morph.start({ ‘opacity’:original}) }
    });
    });
    });

    but I would like my div with “opacity:0.5″ when the page open, and when you click on the div, it stay on “opacity:original” (the link is on the same page with a function show/hide)
    Is that possible ?

    thanks for your answers, and excuse me for my poor english (I’m french :-))

    paps

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!