views:

67

answers:

3

I was wondering, I have links that change blue when a mouse hovers over them. Would it be possible to make them remain blue a few seconds after the mouse has moved away? I'm guessing this would be possible with jquery? Thanks!

+4  A: 

Sure! If you want to have the link fade out, you'll need jQuery UI to animate the color:

$('#myLinkId').hover(
  function(e){
    //mouseover
    $(this).css('color','blue');
  },
  function(e){
    //mouseout
    $(this).animate({color:'black'},300); //300 is the speed of the animation in ms
  }
);

Animate docs: http://api.jquery.com/animate/

Jon Weers
You need jQuery UI to animate colors.
sje397
Good point, thanks! I'll make a note in my answer.
Jon Weers
using another plugin for a simple task? I really think you need jQuery UI on this...
Reigel
Hmm interesting! I can't seem to get this to work. Does this seem Correct? http://pastebin.com/4MYJD3mD (As you can tell, I am noob to jquery. Do i need to install jQuery Ui plugin?
thegreyspot
Yeah, to use my solution you'll need to install the UI plugin, or use the setTimeout version of the mouseOut function posted by Reigel above. You can see it in action here: http://jsfiddle.net/mXkXn/
Jon Weers
CSS3 has the transition rule too, of course it only works in browsers that support it.
AndrewDotHay
+4  A: 

demo

css

a {
  color: red;
}

a.blue {
  color: blue;
}

html

<a href="index.html">home</a>

jQuery

$(document).ready(function(){
   $('a').hover(function(){
       $(this).addClass('blue');
   },
   function(){
       var link = $(this);
       setTimeout(function(){link.removeClass('blue');},1000); 
   })
})

demo

Reigel
Very nice, I'm going to use this!
Pierreten
Any way to make it fade out, rather than just switch back after 1000ms? (Sorry i didnt mention in question)
thegreyspot
@thegreyspot - if that's the case, use Jon Weers answer..
Reigel
+2  A: 

An alternative might be the CSS transition-duration property. This won't keep it a solid color for the specified time but it can allow a transition from one color to another to take a number of seconds for example. This may not be supported by some browsers visiting the page so the other answers using jQuery are great for that.

a {
    color: red;
    transition-duration: 5s;
    -moz-transition-duration: 5s;
    -webkit-transition-duration: 5s;
}

a:hover {
    color: blue;
}
alexcoco
Ya its not that important. This works great in FF and Chrome. Good enough :) thanks!
thegreyspot