【问题标题】:Add fade to this jQuery rollover [duplicate]向此 jQuery 翻转添加淡入淡出 [重复]
【发布时间】:2013-07-01 06:20:24
【问题描述】:

我如何为这段 jQuery 添加淡入淡出或动画不透明度变化?

谢谢

<script  type='text/javascript'>
$(document).ready(function(){
    $(".alain").hover(
        function() {$(this).attr("src","images/rollovers/alain-rollover.png");},
        function() {$(this).attr("src","thumbnails/alain-thumbnail.jpg");
    });
});
</script>

【问题讨论】:

  • 嗯,加.fadeOut().fadeIn()

标签: javascript jquery


【解决方案1】:

淡出图像,更改源,淡入图像:

$(document).ready(function(){
    $(".alain").on('mouseenter mouseleave', function(e) {
        var src = e.type == 'mouseenter' ? 'images/rollovers/alain-rollover.png' : 'thumbnails/alain-thumbnail.jpg';

        $(this).fadeOut('slow', function() {
            $(this).prop('src', src).fadeIn('slow');
        });
    });
});

jsbin

如果旧浏览器不成问题,添加和删除带有 CSS3 过渡的类似乎更容易。

【讨论】:

  • +1 但不是$(".alain").on('mouseenter mouseleave', function(e),你可以写$(".alain").hover(function(e),它会起作用。
猜你喜欢
  • 2011-02-12
  • 1970-01-01
  • 2021-12-02
  • 2011-05-21
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多