Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate opacity on hover (jQuery)

We have a link:

<a href="#">
    Some text
    <span style="width: 50px; height: 50px; background: url(image.png); overflow: hidden; opacity: 0;"></span>
</a>

And we want to change opacity of <span> with some animation, when the link is hovered.

How would we do it?

like image 962
Happy Avatar asked Jan 24 '10 15:01

Happy


3 Answers

Another possible solution:

$("a span").hover(function(){
    $(this).stop().animate({"opacity": 1});
},function(){
    $(this).stop().animate({"opacity": 0});
});

If you use fadeOut(), the span will collapse, this way it won't

EDIT

This is much better:

$('a:has(span)').hover(function() { 
    $('span', this).stop().animate({"opacity": 1}); 
},function() { 
    $('span', this).stop().animate({"opacity": 0}); 
});
like image 88
Raspo Avatar answered Nov 15 '22 09:11

Raspo


Like this:

$('a:has(span)').hover(
    function() { $('span', this).fadeIn(); },
    function() { $('span', this).fadeOut(); }
);
like image 34
SLaks Avatar answered Nov 15 '22 08:11

SLaks


Use .fadeTo():

$( 'a' ).hover(
    function() { $( this ).fadeTo( 'fast', '1'); },
    function() { $( this ).fadeTo( 'fast', '.4'); }
);

Demo: see fiddle

like image 3
Nabil Kadimi Avatar answered Nov 15 '22 08:11

Nabil Kadimi