Change image and fadeIn on hover

旧街凉风 提交于 2019-12-13 18:27:01

问题


I have the following code :

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

How can i add a fadeIn effect while changing the image on hover?


回答1:


Try using fadeIn() and 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>


来源:https://stackoverflow.com/questions/26324493/change-image-and-fadein-on-hover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!