鼠标移动穿过重叠或嵌套区域时的事件计时

发布于 2024-12-20 11:27:35 字数 1143 浏览 7 评论 0原文

当我有嵌套对象时,鼠标经过时发生的事件的时间非常违反直觉且复杂。有人可以解释一下时间规则吗?

例如,当我嵌套 div 时,如下所示:

nested div

其中每个 div 都有onmouseoveronmouseout 属性以便它们触发事件,它们按以下顺序执行:

  • 当鼠标移动 1 --> > 2

    <块引用>

    鼠标悬停2

  • 鼠标移动时2 --> 3

    <块引用>

    鼠标悬停 2
    鼠标悬停 3
    鼠标悬停 2

  • 鼠标移动时 3 --> 4

    <块引用>

    鼠标悬停 3
    鼠标移出 2
    鼠标悬停 4
    鼠标悬停 2
    鼠标悬停 3

  • 鼠标移动时 4 --> 5

    <块引用>

    鼠标悬停 2
    鼠标悬停 5
    鼠标移出 3
    鼠标悬停 4
    鼠标移出 4
    鼠标悬停 3
    鼠标悬停 2

  • 鼠标移动时 5 --> 4

    <块引用>

    鼠标悬停 5
    鼠标移出 4
    鼠标悬停 3
    鼠标悬停 4
    鼠标移出 2
    鼠标移出 3
    鼠标悬停 2

  • 鼠标移动时 4 --> 3

    <块引用>

    鼠标悬停 4
    鼠标悬停 3
    鼠标悬停 2
    鼠标移出 3
    鼠标移开 2

  • 鼠标移动时 3 --> 2

    <块引用>

    鼠标悬停 3
    鼠标移出 2
    鼠标悬停 2

  • 鼠标移动时2 --> 1

    <块引用>

    鼠标悬停 2

我特别使用 Firefox 9.0。请告诉我网络浏览器之间是否存在差异。

When I have nested objects, the timing of events that occur when a mouse passes though is very counterintuitive and complex. Can someone explain the rules for the timing?

For example, when I have nested divs like the following:

nested div

where each div has the onmouseover and onmouseout properties so that they fire events, they do so in the following order:

  • When the mouse moves 1 --> 2

    mouseover 2

  • When the mouse moves 2 --> 3

    mouseout 2
    mouseover 3
    mouseover 2

  • When the mouse moves 3 --> 4

    mouseout 3
    mouseout 2
    mouseover 4
    mouseover 2
    mouseover 3

  • When the mouse moves 4 --> 5

    mouseout 2
    mouseover 5
    mouseout 3
    mouseover 4
    mouseout 4
    mouseover 3
    mouseover 2

  • When the mouse moves 5 --> 4

    mouseout 5
    mouseout 4
    mouseover 3
    mouseover 4
    mouseout 2
    mouseout 3
    mouseover 2

  • When the mouse moves 4 --> 3

    mouseout 4
    mouseover 3
    mouseover 2
    mouseout 3
    mouseout 2

  • When the mouse moves 3 --> 2

    mouseout 3
    mouseout 2
    mouseover 2

  • When the mouse moves 2 --> 1

    mouseout 2

I am particularly using Firefox 9.0. Please let me know if there is difference between webbrowsers.

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

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

发布评论

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

评论(1

就此别过 2024-12-27 11:27:35

我强烈建议您使用 mouseentermouseleave 事件。使用嵌套元素时,鼠标悬停和鼠标移出会很棘手。
来自文档:

与 mouseover 类似,不同之处在于它不会冒泡并且那
直到指针移出其物理空间并且才会发送它
它的所有后代之一。

另外,我建议使用并且我大部分时间都使用 jQuery Hover 帮助器,它绑定那些无错误的冒泡事件。

I highly recommend you to use mouseenter and mouseleave events. Mouseover and mouseout are tricky when using nested elements.
From docs:

Similar to mouseover , it differs in that it doesn't bubble and that
it isn't sent until the pointer has moved from its physical space and
the one of all its descendants.

Also I recommend using and I use most of times jQuery Hover helper which binds those non-buggy-bubbling events.

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