在悬停时更改图像和淡入淡出

时间:2014-10-12 11:08:48

标签: jquery fadein

我有以下代码:

$('div.test').hover(function() {
    $(this).find('img').attr('src', 'img/personale/test2.png');
}, function() {
    $(this).find('img').attr('src', 'img/personale/test.png');
});

如何在悬停时更改图像时添加fadeIn效果?

1 个答案:

答案 0 :(得分:0)

尝试使用fadeIn()fadeOut()



$('div.test').hover(function () {
    $(this).find('img').hide().attr('src', 'http://blog.stackoverflow.com/wp-content/uploads/StackExchangeLogo1.png').fadeIn(2000);
}, function () {
    $(this).find('img').fadeOut(2000,function(){
      $(this).attr('src','http://londontechnologyweek.co.uk/assets/uploads/2014/06/Stack-Overflow-Logo.png').fadeIn();
    });
});

.test{
    min-height:200px;
    text-align:center;
    border:1px solid;
}
.test img{
    width:100%;
    height:auto;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">
    <img src="http://londontechnologyweek.co.uk/assets/uploads/2014/06/Stack-Overflow-Logo.png"/>
</div>
&#13;
&#13;
&#13;