当子元素上的hoverIntent触发时,如何取消父元素上的hoverIntent?
我有一个父元素,当鼠标悬停在其上时会显示一个元素。我还有一个子元素,当鼠标悬停在其上时会显示不同的元素。
我不希望它们同时触发 - 即,如果您将鼠标悬停在子元素上,我只想显示它的关联元素 - 并抑制父元素的悬停。
我很难让它可靠地工作。可能缺少一些明显的东西。有什么想法吗?
编辑 - 说明:
在这种情况下,“父级”和“子级”是独立的可重用组件,彼此不了解,因此我实际上无法将上下文从一个组件注入到另一个组件中
这是我使用 jQuery 设置的演示hoverIntent 插件。
HTML:
<div id="parentBar">
<ul id="childMenu">
<li>Menu 1</li>
</ul>
</div>
<div id="barInfo">
<p>This is shown when hovering overing inside of the parent bar.</p>
</div>
<div id="menuInfo">
<p>This is shown when hovering over inside of the child menu.</p>
</div>
CSS:
#parentBar{width:500px;border:solid 1px black;}
#childMenu{margin-left:10px;padding-left:10px;width:100px;border:solid 1px green;}
#menuInfo, #barInfo{display:none;}
JavaScript:
$('#parentBar').hoverIntent({
//interval: 500,
over: function(e) {
$('#barInfo').show();
},
out: function(e) {
$('#barInfo').hide();
}
});
$('#childMenu').hoverIntent({
//interval: 250,
over: function(e) {
$('#menuInfo').show();
},
out: function(e) {
$('#menuInfo').hide();
}
});
$('#childMenu').bind('mouseenter', function(e){
e.stopPropagation();
});
您可以在 jsFiddle 上查看:http://jsfiddle.net/hNqQ7/1
I have a parent element that when hovered over shows an element. I also have a child element that when hovered over shows a different element.
I don't want them both to fire at the same time - i.e. if you're hovering over the child I only want to show it's associated element - and suppress the hovering over of the parent element.
I'm having trouble getting this to work reliably. Probably missing something obvious. Any ideas?
Edit - clarification:
The "parent" and the "child" in this case are separate reusable components that don't know about each other, so I can't actually inject the context from one into the other
Here is the demo I've set up using jQuery & the hoverIntent plugin.
HTML:
<div id="parentBar">
<ul id="childMenu">
<li>Menu 1</li>
</ul>
</div>
<div id="barInfo">
<p>This is shown when hovering overing inside of the parent bar.</p>
</div>
<div id="menuInfo">
<p>This is shown when hovering over inside of the child menu.</p>
</div>
CSS:
#parentBar{width:500px;border:solid 1px black;}
#childMenu{margin-left:10px;padding-left:10px;width:100px;border:solid 1px green;}
#menuInfo, #barInfo{display:none;}
JavaScript:
$('#parentBar').hoverIntent({
//interval: 500,
over: function(e) {
$('#barInfo').show();
},
out: function(e) {
$('#barInfo').hide();
}
});
$('#childMenu').hoverIntent({
//interval: 250,
over: function(e) {
$('#menuInfo').show();
},
out: function(e) {
$('#menuInfo').hide();
}
});
$('#childMenu').bind('mouseenter', function(e){
e.stopPropagation();
});
You can view it here on jsFiddle: http://jsfiddle.net/hNqQ7/1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在子事件(两个函数)中,调用 e.stopPropagation()
编辑:
好吧,抱歉,我之前没有看过演示。您可以将子代码更改为以下内容:
In the child event (both functions), call e.stopPropagation()
Edit:
Ok, sorry, I didn't look at the demo previously. You can change the child code to the following:
该解决方案修改了核心插件。
使用最新版本的hoverIntent, https://github.com/briancherne/jquery-hoverIntent ,我对由 mouseenter 触发的核心函数 compare 进行了简单的修改。
查找(~第 65 行):
并替换为:
这允许您从子级/单独的交互中创建任何逻辑,并通过切换类 preventIntent 来抑制悬停意图的“over”功能。
示例:
或者,可以使用“ignoreChild”选项选择器改进核心插件,该选择器在每个循环上执行 .is(":hover") 检查。这可以减少额外鼠标事件的需要,但显然将其限制为悬停。
This solution modifies the core plugin.
Using the latest version of hoverIntent, https://github.com/briancherne/jquery-hoverIntent , I made a simple modification to the core function, compare, triggered from mouseenter.
Find (~line 65):
And replace with:
This allows you to create any logic from children/separate interactions and suppress the hoverIntent's "over" function by toggling the class preventIntent .
Example:
Alternatively, the core plugin could be improved with an "ignoreChild" option selector, that performs a check of .is(":hover") on each loop. This could reduce the need of extra mouse events, but restricts it to hovers only obviously.