10

我们有一个链接:

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

<span>当链接悬停时,我们想用一些动画改变不透明度。

我们会怎么做?

4

3 回答 3

46

另一种可能的解决方案:

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

如果你使用fadeOut(),span会崩溃,这样就不会

编辑

这要好得多:

$('a:has(span)').hover(function() { 
    $('span', this).stop().animate({"opacity": 1}); 
},function() { 
    $('span', this).stop().animate({"opacity": 0}); 
});
于 2010-01-24T17:38:45.837 回答
9

像这样:

$('a:has(span)').hover(
    function() { $('span', this).fadeIn(); },
    function() { $('span', this).fadeOut(); }
);
于 2010-01-24T15:13:42.963 回答
3

使用.fadeTo()

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

演示:见小提琴

于 2014-10-13T17:53:40.243 回答