忽略重叠 div 的鼠标事件
一个页面有 4 个 div:
- 项目 A
- 项目 B
- 项目 C
- 详细信息(隐藏)
所需的效果是,当鼠标悬停在项目框上时,将显示详细信息框,而在鼠标移开后隐藏。显示时,详细信息框将与项目框右侧约 20% 重叠。
然而,我当前得到的错误效果是,当我将鼠标悬停在任何项目框的右边缘时,它会进入显示和隐藏详细信息框的永久循环。大概这是因为它触发了项目框的 mouseout 事件(当显示详细信息时),但当隐藏详细信息框时立即再次触发鼠标悬停事件。我想知道如何解决这个问题。
我当前的代码是:
$('div.item').hover(
function() {
$(this).showDetails();
},
function() {
$(this).hideDetails();
}
);
提前感谢您的指点! 这是 jsfiddle 链接,供无法想象的人使用。尝试将鼠标悬停在右侧的项目框上,然后稍微移动鼠标,您将看到闪烁。
Xavier,我已经尝试过使用appendTo获取详细信息框,但我想要的效果是我希望鼠标事件仅针对可操作的 div(即 Item)。经过一番研究后,我什至非常确定这是否可能,因为“详细信息”框覆盖了 div(因此无法将事件附加到其下方的某些内容)
A page has 4 divs:
- Item A
- Item B
- Item C
- Details (hidden)
The desired effect is that the Details box will be shown when hovering over the Item box, and be hidden once moused out. The Details box will overlap with about 20% of the right of the Item box when it is shown.
However, the current erroneous effect I'm getting is that when I hover just on the right edge of any of the Item boxes, it goes into a forever loop of showing and hiding the Details box. Presumably this is because it's triggering the mouseout event of the Item box (when Details gets displayed) but immediately triggering the mouseover event again when the Details box is hidden. I'd like to know how to get around this.
My current code is:
$('div.item').hover(
function() {
$(this).showDetails();
},
function() {
$(this).hideDetails();
}
);
Thanks in advance for any pointers!
Here's the jsfiddle link for anyone that can't visualize it. Try hovering over the Item boxes on the right side, and move the mouse a little and you will see the flicker occuring.
Xavier, I've already tried appendTo for the details box, but the effect I want is that I want to have the mouse events only targeted to the actionable div (namely, Item). After some research I'm even quite sure if this is possible since the Details box is covering the div (and thus not able to attach an event to something below it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种解决方法是让详细信息 DIV 成为您将鼠标悬停在其上的项目的子项。这样,当指针位于详细信息 DIV 上时,指针仍然位于正确的项目“内部”,并且在鼠标离开两个 DIV 之前不会发送 mouseout。您至少可以通过两种方式做到这一点:
您必须执行一些 CSS 技巧才能使其按您想要的方式显示,但只要存在父子关系,您就不必担心错误的鼠标事件。
希望这有帮助!
One workaround would be to have the details DIV be a child of the item you're hovering over. That way the pointer is still "in" the correct item when it's over the details DIV, and mouseout isn't sent until the mouse leaves both DIVs. You could do this at least two ways:
You'll have to do some CSS trickery to get it to display like you want, but as long as the parent-child relationship is there, you shouldn't have to worry about erroneous mouse events.
Hope this helps!
我无法想象你的问题。目前尚不清楚详细信息框是否在所有情况下看起来都相同,以及页面上的项目 div 位于何处以及彼此之间的关系。
请您为其创建一个 JSFiddle 并在其中粘贴一些 CSS、HTML 和 JS。那我可以看一下。
I am having trouble visualizing your problem. Its unclear if the details box looks the same in all cases and where are the item divs on the page, and in relation to each other.
Please can you create a JSFiddle for it and paste some CSS, HTML and JS in there. Then I can take a look.
试试这个:
我想这就是您要寻找的:
HTML:
JAVASCRIPT:
点击此处查看示例
Try this one:
I think is what yr looking for:
HTML:
JAVASCRIPT:
CLICK HERE TO SEE THE SAMPLE
Nando,那里有一些更好的答案,但我只是想把它放在那里,尽管我确信你已经尝试过。
特别是因为您正在使用悬停(),听起来有点像您遇到了一个非常简单的问题。
我确定您尝试过 stop(true, true) 对吗?
Nando, there are some better answers up there but I just want to throw this up there, though I am sure that you tried it.
Specially since you are using hover(), it sounds a little like you are running into what could be a very simple problem.
I'm sure you tried stop(true, true) right?