views:

1033

answers:

3

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?

+3  A: 

Like this:

$('a:has(span)').hover(
    function() { $('span', this).fadeIn(); },
    function() { $('span', this).fadeOut(); }
);
SLaks
+1  A: 

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}); 
});
Raspo
Your selector is wrong - he wants the effect to appear when the link is hovered, not the span. However, you have a good point.
SLaks
You have a good point too.
Raspo
A: 

great! this helped me a lot too. Thanks Raspo!

Jim