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
2010-07-02 04:00:50
You need jQuery UI to animate colors.
sje397
2010-07-02 04:02:08
Good point, thanks! I'll make a note in my answer.
Jon Weers
2010-07-02 04:07:12
using another plugin for a simple task? I really think you need jQuery UI on this...
Reigel
2010-07-02 04:09:06
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
2010-07-02 04:27:27
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
2010-07-02 04:31:44
CSS3 has the transition rule too, of course it only works in browsers that support it.
AndrewDotHay
2010-07-02 04:37:06
+4
A:
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);
})
})
Reigel
2010-07-02 04:03:04
Any way to make it fade out, rather than just switch back after 1000ms? (Sorry i didnt mention in question)
thegreyspot
2010-07-02 04:28:17
+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
2010-07-02 04:40:59
Ya its not that important. This works great in FF and Chrome. Good enough :) thanks!
thegreyspot
2010-07-02 14:27:31