use*_*109 13 javascript jquery image
如何使用jquery将一个图像淡入另一个图像?据我所知,你会使用fadeOut,用attr()更改源,然后再次使用fadeIn.但这似乎并不合适.我不想使用插件,因为我希望添加相当多的更改.
谢谢.
Rya*_*ary 30
在最简单的情况下,您需要在调用fadeOut()时使用回调.
假设页面上已有图片标记:
<img id="image" src="http://sstatic.net/so/img/logo.png" />
Run Code Online (Sandbox Code Playgroud)
您将一个函数作为回调参数传递给fadeOut()那个重置该src属性,然后使用fadeIn()以下方法淡化:
$("#image").fadeOut(function() {
$(this).load(function() { $(this).fadeIn(); });
$(this).attr("src", "http://sstatic.net/su/img/logo.png");
});
Run Code Online (Sandbox Code Playgroud)
对于jQuery中的动画,回调在动画完成后执行.这使您能够按顺序链接动画.请注意拨打电话load().这样可以确保在淡入之前加载图像(感谢Y. Shoham).
好吧,您可以将下一个图像放在当前图像后面,然后淡出当前图像,使其看起来好像正在淡入下一个图像.
淡出完成后,您可以换回图像.粗略地说:
<style type="text/css">
.swappers{
position:absolute;
width:500px;
height:500px;
}
#currentimg{
z-index:999;
}
</style>
<div>
<img src="" alt="" id="currentimg" class="swappers">
<img src="" alt="" id="nextimg" class="swappers">
</div>
<script type="text/javascript">
function swap(newimg){
$('#nextimg').attr('src',newimg);
$('#currentimg').fadeOut(
'normal',
function(){
$(this).attr('src', $('#nextimg').attr('src')).fadeIn();
}
);
}
</script>
Run Code Online (Sandbox Code Playgroud)
$("#main_image").fadeOut("slow",function(){
$("#main_image").load(function () { //avoiding blinking, wait until loaded
$("#main_image").fadeIn();
});
$("#main_image").attr("src","...");
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39462 次 |
| 最近记录: |