开发者

Animate opacity on hover (jQuery)

开发者 https://www.devze.com 2022-12-17 04:26 出处:网络
We have a link: <a href=\"#\"> Some text <span style=开发者_如何学编程\"width: 50px; height: 50px; background: url(image.png); overflow: hidden; opacity: 0;\"></span>

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?


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 this:

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


Use .fadeTo():

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

Demo: see fiddle

0

精彩评论

暂无评论...
验证码 换一张
取 消