views:

5978

answers:

7

There is a link, with no background, and a css rule, which changes background on hover.

Parent bg is white, link on hover - blue.

How can I do a hover effect slowly, from white to blue?

Thanks.

li a {}
li a:hover { background: blue; }
+1  A: 

In order to animate colors, you need the color plugin

Jason Punyon
+7  A: 
jQuery('a#theLink').hover(function(){
    $(this).stop().animate({backgroundColor: 'blue'});
}, function() {
    $(this).stop().animate({backgroundColor: 'white'});
});

For this to work you need to download the "color plugin" for jQuery.

J-P
+1  A: 

You'll need to use a plugin as JQuery can't animate colours out of the box.

Try the Colour Animation plugin

Then it'll be something like:

$(this).animate({ backgroundColor: "blue" }, 'slow');
Fermin
A: 

Thanks, and one more, how can I do same effect if I have an image background instead of simple color?

+1  A: 

You don't need an extra color plugin. You just need jQuery UI on top of jQuery, which lets you do animations also on color. (Or if you don't want to use jQuery UI for some reason, I guess another plugin will do the trick. But I have successfully tried this with just including jQuery core and jQuery UI.)

Animation itself goes something like...

$("li").hover(function() {
  $(this).animate({
    backgroundColor: "#ffffff"
  }, 'slow');
});
Jaanus
I know file sizes aren't too much of an issue these days at the level of kilobytes (although stuff does add up), but just for the record: jquery.color.js - 4k | jquery-ui-1.8rc3.custom.min.js - 64k
Dave Everitt
Stating the obvious, since no one else has, if you are already using jQuery UI you are good to go. If you are not using jQuery UI then don't start using it just for this feature -- use the color plugin.
MikeJ
A: 

bgFade a very great and simple plugin that animates de background image:

http://www.isthatclear.com/jquery/bgFade/

tothemario
A: 

jQuery UI heavy on the file size compared to a few kb plug in

Rob