具有 jQuery 图像翻转效果的无尽淡入淡出循环
我的意图是用漂亮的淡入淡出效果替换图像: 我有一张图像 A 作为背景。鼠标悬停时,图像 B 淡入。 鼠标移开时,图像 B 淡出,我们可以再次看到图像 A。 我正在使用这段代码:
<script type='text/javascript'>
$(function() {
$("img.fade")
.mouseover(function() {
$(this).fadeOut(2000);
})
.mouseout(function() {
$(this).fadeIn(2000);
});
});
</script>
但问题是当用户悬停时,它会继续循环(淡入,淡出,淡入,淡出..)。我希望当褪色完成时它保持不变。当用户鼠标移出时,我希望新的淡入淡出将会发生。 谢谢!
附注 这是使用 2 个图像的工作代码。这是问题的不同解决方案,我在问题解决后添加此解决方案。
<script type='text/javascript'>
$(function() {
$('img.fade').hover(function() {
var src = $(this).attr("src").match(/[^\.]+/) + "_over.jpg";
$(this)
.animate({opacity:0},0)
.attr('src',src)
.stop()
.animate({opacity:1},1000);
}, function() {
var src = $(this).attr("src").replace("_over", "");
$(this)
.animate({opacity:0},0)
.attr('src',src)
.stop()
.animate({opacity:1},500);
});
});
</script>
my intension was to replace images with nice fade effect:
i have one image A as background. on mouse hover, image B fadeIn.
on mouse out, image B fadeOut and we can see image A again.
i'm using this code:
<script type='text/javascript'>
$(function() {
$("img.fade")
.mouseover(function() {
$(this).fadeOut(2000);
})
.mouseout(function() {
$(this).fadeIn(2000);
});
});
</script>
but the problem is that when the user stay on hover, it continue to loop (fadeIn, fadeOut,fadeIn,fadeOut..). i want that when the fade finish it holds. when the user mouse out, just then i want that the new fade will happen.
Thanks!
p.s
this is working code for using 2 images. this is different solution to the problem and i adsd this after my question is resolved.
<script type='text/javascript'>
$(function() {
$('img.fade').hover(function() {
var src = $(this).attr("src").match(/[^\.]+/) + "_over.jpg";
$(this)
.animate({opacity:0},0)
.attr('src',src)
.stop()
.animate({opacity:1},1000);
}, function() {
var src = $(this).attr("src").replace("_over", "");
$(this)
.animate({opacity:0},0)
.attr('src',src)
.stop()
.animate({opacity:1},500);
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这段代码:
问题是 fadeOut() 函数将元素的 display 属性设置为 none,因此 DOM 认为您的鼠标不再与该元素交互,并调用 fadeIn() 函数。它不断循环。 fadeTo 函数改变不透明度,但它实际上并没有使图像移动。它需要两个参数,持续时间和不透明度。
Try this code:
The problem is that the fadeOut() function sets the display property of your element to none, so the DOM thinks that your mouse is no longer interacting with the element, and calls the fadeIn() function. It loops continuously. The fadeTo function changes opacity, but it does not actually make the image go a way. It takes two paramaters, duration and opacity.
在我看来,图像一旦淡出就会消失,这会触发鼠标移出功能。尝试将动画设置为 0.01 不透明度。
Seems to me the image disappears once it fades out, which would trigger the mouseout function. Try animating to .01 opacity.
您可以此处尝试一下
参考: .hover() , .stop()
You can try it here
Ref : .hover() , .stop()
如果您不想动态切换图像并且真的想要继续使用背景图像,您可以利用事件冒泡...
HTML:
jQuery:
未经测试,因此请告诉我们它是否有效。
If you don't want to dynamically switch the image and REALLY want to carry on using a background image you could take advantage of event bubbling...
HTML:
jQuery:
Untested so let us know if it works or not.
对于其他人也在这里通过
mouseenter + mouseleave 可能有助于解决您认为可能有效的奇怪的半循环行为,例如
似乎 mouseover 和 mouseout 比您想象的更具包容性,例如 mouseout 包括子元素。
请参阅演示部分 http://api.jquery.com/mouseover/
For others also led here by
mouseenter + mouseleave might help with an odd semi-looping behaviour you thought might work e.g.
Seems mouseover and mouseout are more inclusive than you might think e.g. mouseout includes child elements.
See demo section http://api.jquery.com/mouseover/