2016-02-07 143 views
0

因此,使用jQuery我使用img.attr('src', 'img.png')更改图像的来源。jQuery:如何使用img.attr('src','img.png')动画(淡入淡出)改变图像源?

这只是改变源代码,并以一种非常静态的方式更新图像,而不会执行任何动画,如淡入淡出。

我该如何为图像的这种变化设置动画?

+0

你更换一个图像的src或者是没有一个src一个新创建的图像元素? –

+1

重复:[jQuery更改图像src与淡入淡出效果](http://stackoverflow.com/questions/5979418/jquery-change-image-src-with-fade-effect) – Roberto

+0

替换源:) – Shuzheng

回答

2

希望这会有所帮助。 当我遇到这个问题时,看到了这个解决方案。

$("#link").click(function() { 
 
    $("#image").fadeOut(1000, function() { 
 
     $("#image").attr("src",$("#link").attr("href")); 
 
    }).fadeIn(1000); 
 
    return false; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<img id="image" src="http://www.google.com/logos/2011/trevithick11-hp.jpg"/> 
 
<a id="link" href="http://www.google.com/logos/2011/mothersday11-hp.jpg">Change picture</a>

1

淡出imgage和,而形象是无形的,改变它的来源:

$('#myImg').fadeOut().queue(function() { 
    var img = $(this); 
    img.attr('src', newSrc); 
    img.fadeIn(); 
    img.dequeue(); 
});