Jquery - 如何修复此图像翻转?
大家好,我有这个小 Jquery 脚本: 链接文本
$(document).ready(function()
{
$('#image p').hide();
$('img').hover(function()
{
$('#image p').show(200);
}, function()
{
$('#image p').hide(200);
});
});
I工作正常,但我希望能够将鼠标悬停在图像中的文本上,每次我尝试时,它都会保持“弹跳”
任何帮助都非常感谢, 谢谢, 基思
Hi Guys I have this little Jquery script: link text
$(document).ready(function()
{
$('#image p').hide();
$('img').hover(function()
{
$('#image p').show(200);
}, function()
{
$('#image p').hide(200);
});
});
I works fine, but I want to be able to hover over the Text located in the Image, every time I try, it just keeps "bouncing"
Any help is much appreciated,
thanks,
Keith
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好问题。
问题似乎是,当鼠标悬停在段落上时,鼠标不再悬停在图像上。 所以该段落被隐藏了。 当该段落被隐藏时,鼠标再次位于图像上,因此该段落再次显示。 依此类推...
一个好的解决方案是使用 mouseenter 和 mouseleave 事件而不是 mouseover 和 mouseout:
mouseenter/mouseleave 事件和 mouseover/mouseout 事件之间的主要区别是前者不会起泡。
在此示例中,div#image 的子段落仍然接收 mouseenter/mouseleave 事件(即使您没有监听它们),但这些事件不会冒泡到其父元素。 请参阅 此页之前对此进行了广泛的讨论。
您还必须不再将事件分配给 img 标签,而是分配给包含的 div。 这应该不是问题。
Good question.
The problem seems to be that when the mouse is over the paragraph, the mouse is no more over the image. So the paragraph is hidden. When the paragraph is hidden, the mouse is again over the image, and so the paragraph is shown again. And so on...
A good solution is to use mouseenter and mouseleave events instead of mouseover and mouseout:
The major difference between mouseenter/mouseleave events and mouseover/mouseout events is that the former don't bubble.
In this example, the child paragraph of div#image still receive the mouseenter/mouseleave events (even if you aren't listening for them) but the events won't bubble up to their parent element. See this page fore extended discussion on it.
You also have to assign the event no more to the img tag, but to the containing div. It should not be a problem.