Javascript 鼠标悬停从孩子冒泡

发布于 2024-08-25 08:15:30 字数 900 浏览 7 评论 0原文

我有以下 html 设置:

<div id="div1">
<div id="content1">blaat</div>
<div id="content1">blaat2</div>
</div>

它的样式设置为您不能在不悬停其他 2 个 div 之一的情况下悬停 div1。 现在我在 div1 上有了鼠标悬停。
问题是当我从 content1 移动到 content2 时,我的 div1.mouseout 被触发,因为它们的 mouseout 正在冒泡。
并且事件的目标、currentTarget 或 relatedTarget 属性永远不会是 div1,因为它永远不会直接悬停...
我一直在疯狂地寻找这个,但我只能找到与我需要的相反的问题的文章和解决方案。这看起来微不足道,但我无法让它工作......
div1 的 mouseout 仅应在鼠标离开 div1 时触发。

一种可能性是在鼠标输入和鼠标离开上设置一些数据,但我相信这应该开箱即用,因为它只是一个鼠标离开...

编辑:

bar.mouseleave(function(e) {
                if ($(e.currentTarget).attr('id') == bar.attr('id')) {
                    bar.css('top', '-'+contentOuterHeight+'px');
                    $('#floatable-bar #floatable-bar-tabs span').removeClass('active');
                }
            });

将鼠标输出更改为鼠标离开并且代码有效。 ..

Ive got the following html setup:

<div id="div1">
<div id="content1">blaat</div>
<div id="content1">blaat2</div>
</div>

it is styled so you can NOT hover div1 without hovering one of the other 2 divs.
Now i've got a mouseout on div1.
The problem is that my div1.mouseout gets triggered when i move from content1 to content2, because their mouseouts are bubbling.
and the event's target, currentTarget or relatedTarget properties are never div1, since it is never hovered directly...
I've been searching mad for this, but I can only find articles and solutions for problems who are the reverse of what I need. It seems trivial but I can't get it to work...
The mouseout of div1 should ONLY get triggered when the mouse leaves div1.

One of the possibilities would be to set some data on mouse enter and mouseleave, but I'm convinced this should work out of the box, since it is just a mouseout...

EDIT:

bar.mouseleave(function(e) {
                if ($(e.currentTarget).attr('id') == bar.attr('id')) {
                    bar.css('top', '-'+contentOuterHeight+'px');
                    $('#floatable-bar #floatable-bar-tabs span').removeClass('active');
                }
            });

changed the mouseout to mouseleave and the code worked...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

当梦初醒 2024-09-01 08:15:30

为此,请使用 mouseleave 事件或 mouseout 来处理您的特定问题。 请参阅此处了解详细信息

来自有关差异的文档:

mouseleave 事件与 mouseout 事件的不同之处在于它处理事件冒泡的方式。如果本例中使用了 mouseout,那么当鼠标指针移出 Inner 元素时,就会触发处理程序。这通常是不受欢迎的行为。另一方面,mouseleave 事件仅在鼠标离开其绑定的元素(而不是后代元素)时触发其处理程序。因此,在此示例中,当鼠标离开 Outer 元素而不是 Inner 元素时,将触发处理程序。

标记示例:

<div id="outer">
  Outer
  <div id="inner">
    Inner
  </div>
</div>

Use the mouseleave event instead or mouseout for this, it handles your specific issue. See here for details

From the docs on the difference:

The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. This is usually undesirable behavior. The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element.

Example markup:

<div id="outer">
  Outer
  <div id="inner">
    Inner
  </div>
</div>
皓月长歌 2024-09-01 08:15:30

hover 方法有两个参数,第一个参数用于鼠标进入,第二个参数用于鼠标移出。

$('your_div').hover(function(){
  // your code here.
}, function(){// any mouse out code here})

The hover method has two parameters, first for mouse in and second for mouse out.

$('your_div').hover(function(){
  // your code here.
}, function(){// any mouse out code here})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文