Fading Links Using jQuery: dwFadingLinks
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
});
});
The HTML5 revolution has provided us some awesome JavaScript and HTML APIs. Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers. Regardless of API strength or purpose, anything to help us better do our job is a...
CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals. Add animation and you've got something really neat. Unfortunately each CSS cube tutorial I've read is a bit...
I was recently scoping out the horrid source code of the Google homepage when I noticed the "Google Search" and "I'm Feeling Lucky" buttons had a style definition I hadn't seen before: -webkit-appearance. The value assigned to the style was "push-button." They are buttons so that...
One of my favorite Firefox plugins is called LinkAlert. LinkAlert shows the user an icon when they hover over a special link, like a link to a Microsoft Word DOC or a PDF file. I love that warning because I hate the surprise...
I think this code has potential for abuse by the webmasters.
Hahahaha. No way — webmasters couldn’t use this because they use applets for links.
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???
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??
Awesome, how ever I would use $(this).stop().animate $(this).animate to make it a bit smother if you glide the mouse over.
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.
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
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 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. :)
Sexy script!
Nice script but couldn’t figure out why the color and duration was used twice. In the end I took out this portion:
and it still worked fine..
nice and simple
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.
Very nice – I like this execution. Here’s a question: How could you keep the link you click to stay highlighted using this code?
Doesn’t work in Chrome :(
Not surprised, even simple radio buttons dont seem to work on Chrome.
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!!hi, beautiful script. what i have to edit, to change background-color instead color? thanks
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?
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 :)
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.
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!
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
Is there some way to make this work with normal css
:hover
definitions ? Or at least to over-ride them ? Because, i don’t want my links to be without any hover effect when JS is disabled.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)
:So they should look like this together:
That said, I’m a very novice scripter—this added line from me was little more than an educated guess.
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 !!!
Works kinda like this: http://patrickdevivo.com/jquery-color-change/
How can i integrate this in phpbb3 ?
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.
I solved that hover problem for me. My links now have the class “css” with the ususal settings for color in case Javascript should be disabled.
Then I tweaked daves script like this:
Now, if javascript is enabled, jQuery removes the class “css” from my links and thus disables the css-hover.
Seems to work!
Oh yeah, I´m a f00! Haha. That link should look like this
link
Could anyone tell me if there is a way to tweak the javascript so that the fade in is instant (like a regular hover) but the fade out is the animated fade?
Thanks!
I have one problem, because when I insert the code, the effect is applied and the image-link … and it’s not good … but the effect is excellent. I do not know if I need something to change the js?
This was a great post, thanks for the info.
Hello, i think which i saw you frequented my own blog site so i found ???return the favor??à.I will be trying to find things to boost my personal web site!I guess its ok to utilize some of your ideas!!
I’ve try to applied this to an image, but it’s not working. How can I make this works for images?
…or you just use css3:
Very nice info. Thanks for sharing
Very effective – tested and works perfectly! With a little adjustment JS links really look nice on the website. Thank you.
Cleaning JS code – Google did not make the problem W3Org validation is passed to the JS padding Links. A great thing.