2014-10-12 74 views
1

我有以下代碼:變化圖像和淡入懸停

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

而懸停改變形象我怎麼能添加淡入效果?

+0

你爲什麼鼠標離開'src'設置爲相同的圖像? – 2014-10-12 11:14:07

+0

對不起我的壞,firast是test2:D – roleplaynoex 2014-10-12 11:24:12

回答

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>