悬停时的动画不透明度(jQuery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2127458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:49:55  来源:igfitidea点击:

Animate opacity on hover (jQuery)

jqueryanimationhoveropacity

提问by Happy

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.

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

How would we do it?

我们会怎么做?

采纳答案by SLaks

Like this:

像这样:

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

回答by Raspo

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

如果你使用fadeOut(),span 将会折叠,这样它就不会

EDIT

编辑

This is much better:

这要好得多:

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

回答by Nabil Kadimi

Use .fadeTo():

使用.fadeTo()

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

Demo: see fiddle

演示:见小提琴