我有jquery脚本,当有人将鼠标移到图像上时,它会替换图像。
我想让它随着淡入淡出而改变。
我该怎么办?
谢谢!
<img src='images/dogySitter_s.png' alt='' id='first' />
<script type='text/javascript'>
$(document).ready(function(){
$("#first").hover(
function() {$(this).attr("src","images/dogySitter_c.png");},
function() {$(this).attr("src","images/dogySitter_s.png");
});
});
</script>发布于 2013-08-24 06:37:48
这将淡出元素,交换图像,并淡入元素(设置不透明度动画将避免在元素淡出时触发mouseleave的问题):
$(document).ready(function(){
$("#first").on({
mouseenter: function() {
$(this).animate({opacity: 0}, 400, function() {
$(this).attr('src', 'images/dogySitter_c.png')
.animate({opacity: 1}, 400);
});
},
mouseleave: function() {
$(this).animate({opacity: 0}, 400, function() {
$(this).attr('src', 'images/dogySitter_s.png')
.animate({opacity: 1}, 400);
});
}
});
});对于交叉淡入淡出,你需要两个元素
发布于 2013-08-24 06:40:36
adeneo的回答提供了fadeIn函数作为fadeOut函数的回调。如果你想让图像淡出,然后让另一个图像淡入,这将非常有效。
如果你想让淡入淡出同时发生,你可能需要使用一些css技巧将隐藏的图片放在非隐藏的图片上,然后紧接着调用fadeIn和fadeOut。所有jQuery动画都是异步执行的,因此它们将同时发生。
有关更多信息,请访问fadeOut here http://api.jquery.com/fadeOut/
https://stackoverflow.com/questions/18412872
复制相似问题