当子元素上的hoverIntent触发时,如何取消父元素上的hoverIntent?

发布于 2024-11-09 20:46:51 字数 1604 浏览 4 评论 0原文

我有一个父元素,当鼠标悬停在其上时会显示一个元素。我还有一个子元素,当鼠标悬停在其上时会显示不同的元素。

我不希望它们同时触发 - 即,如果您将鼠标悬停在子元素上,我只想显示它的关联元素 - 并抑制父元素的悬停。

我很难让它可靠地工作。可能缺少一些明显的东西。有什么想法吗?

编辑 - 说明:

在这种情况下,“父级”和“子级”是独立的可重用组件,彼此不了解,因此我实际上无法将上下文从一个组件注入到另一个组件中

这是我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-小熊_ 2024-11-16 20:46:52
var flag = false;
$('#parentBar').hoverIntent({
  interval: 500,
  over: function(e) {
    if(!flag) {
        $('#barInfo').show();
    }
  },
  out: function(e) {
    $('#barInfo').hide();
  }
});

$('#childMenu').hoverIntent({
  interval: 250,
  over: function(e) {
    $('#menuInfo').show();
  },
  out: function(e) {
    $('#menuInfo').hide();
  }
}).mouseenter(function(){
    flag= true;
}).mouseleave(function(){
    flag = false;
});
var flag = false;
$('#parentBar').hoverIntent({
  interval: 500,
  over: function(e) {
    if(!flag) {
        $('#barInfo').show();
    }
  },
  out: function(e) {
    $('#barInfo').hide();
  }
});

$('#childMenu').hoverIntent({
  interval: 250,
  over: function(e) {
    $('#menuInfo').show();
  },
  out: function(e) {
    $('#menuInfo').hide();
  }
}).mouseenter(function(){
    flag= true;
}).mouseleave(function(){
    flag = false;
});
恰似旧人归 2024-11-16 20:46:52

在子事件(两个函数)中,调用 e.stopPropagation()

编辑:
好吧,抱歉,我之前没有看过演示。您可以将子代码更改为以下内容:

$('#childMenu').hoverIntent({
   interval: 250,
   over: function(e) {
        $('#barInfo').hide();
        $('#menuInfo').show();
    },
    out: function(e) {
        $('#barInfo').show();
        $('#menuInfo').hide();
    }
});

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:

$('#childMenu').hoverIntent({
   interval: 250,
   over: function(e) {
        $('#barInfo').hide();
        $('#menuInfo').show();
    },
    out: function(e) {
        $('#barInfo').show();
        $('#menuInfo').hide();
    }
});
丢了幸福的猪 2024-11-16 20:46:52

该解决方案修改了核心插件。

使用最新版本的hoverIntent, https://github.com/briancherne/jquery-hoverIntent ,我对由 mouseenter 触发的核心函数 compare 进行了简单的修改。

查找(~第 65 行):

if (Math.sqrt((s.pX - cX) * (s.pX - cX) + (s.pY - cY) * (s.pY - cY)) < cfg.sensitivity) {

并替换为:

if (!$el.hasClass("preventIntent") && Math.sqrt((s.pX - cX) * (s.pX - cX) + (s.pY - cY) * (s.pY - cY)) < cfg.sensitivity) {

这允许您从子级/单独的交互中创建任何逻辑,并通过切换类 preventIntent 来抑制悬停意图的“over”功能。

示例:

$(".child-ele").on("mouseenter", function(e) {
    $(this).closest(".parent-ele").addClass("preventIntent");
}).on("mouseleave", function(e) {
    $(this).closest(".parent-ele").removeClass("preventIntent");
});

或者,可以使用“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):

if (Math.sqrt((s.pX - cX) * (s.pX - cX) + (s.pY - cY) * (s.pY - cY)) < cfg.sensitivity) {

And replace with:

if (!$el.hasClass("preventIntent") && Math.sqrt((s.pX - cX) * (s.pX - cX) + (s.pY - cY) * (s.pY - cY)) < cfg.sensitivity) {

This allows you to create any logic from children/separate interactions and suppress the hoverIntent's "over" function by toggling the class preventIntent .

Example:

$(".child-ele").on("mouseenter", function(e) {
    $(this).closest(".parent-ele").addClass("preventIntent");
}).on("mouseleave", function(e) {
    $(this).closest(".parent-ele").removeClass("preventIntent");
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文