Javascript:[ onmouseover ] 和 [ onmouseout ] 事件

发布于 2024-09-16 15:21:52 字数 3287 浏览 5 评论 0原文

一般问题


例如,假设有一个下拉菜单,当您将鼠标悬停在链接上时,会弹出下拉菜单。

但正如您在下面的文章中所读到的那样,它存在问题,当您将鼠标悬停在链接上时(对于某些浏览器,元素内的所有内容),该框都会消失。问题来自于事件冒泡

在我的文档中,onmouseoveronmouseout延迟了 0.5 秒,您可以看到有时元素会因为这个问题而开始振动。

--------------
| Layer      |.onmouseout = doSomething;
| --------   |
| | Link | ----> We want to know about this mouseout
| |      |   |
| --------   |
| --------   |
| | Link |   |
| |    ----> | but not about this one
| --------   |
--------------
---->: mouse movement

阅读本文以更好地理解:

www.quirksmode .org - Javascript - 鼠标事件

Quirksmode 解决方案


function doSomething(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    if (tg.nodeName != 'DIV') return;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    while (reltg != tg && reltg.nodeName != 'BODY')
        reltg= reltg.parentNode
    if (reltg== tg) return;
    // Mouseout took place when mouse actually left layer
    // Handle event
}

我的文档


您可以在这里找到我的完整文档:

JS Bin - 我的文档

您可以在这里找到不带 mouseEvent(e) 函数的原始文档:

JS Bin - Original

将鼠标悬停在按钮上,然后将鼠标悬停在框上,然后快速将鼠标移出并快速返回到然后盒子就会开始振动。 (在 Firefox 3.6 Windows 7 上)

Javascript

<script type="text/javascript">
function mouseEvent(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    if (tg.nodeName != 'DIV') return;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    while (reltg != tg && reltg.nodeName != 'BODY')
        reltg= reltg.parentNode
    if (reltg== tg) return;
    // Mouseout took place when mouse actually left layer
    // Handle event
}
function toggleByType(id, type, e){
    setTimeout(function(){
        var element = document.getElementById(id);
        if(element.style.display == type){
            mouseEvent(e);
            element.style.display = 'none'; 
        } else {
            element.style.display = type;   
        }
    }, 500);
}
</script>

HTML

<div class="box-container" onmouseover="toggleByType('box','block');" onmouseout="toggleByType('box','block', event);">
    <a href="#" class="box-bridge">Show Box</a>
    <div id="box" class="box" style="display:none;">
            Mouse out and this will dissapear.
            <br />
            <a href="#">Roll over to have problems</a>
        </div>
</div>

我的问题


quirksmode 给出的解决方案听起来很合乎逻辑,但我不这么认为不知道如何使用该功能我尝试了很多方法,我发布的只是其中一种,但我不明白,所以如果你能帮助我,我会很高兴这项工作。

The General Problem


Imagine a dropdown menu for example and when you mouse over on a link the dropdown is popping up.

But as you can read on the article below, there are problems with it, when you mouse over on a link(for some browsers everything inside the element) the box is dissapearing. The problem comes from event bubbling.

In my document onmouseover and onmouseout are delayed with 0.5 seconds and you can see that sometimes the element starts to vibrate, because of this problem.

--------------
| Layer      |.onmouseout = doSomething;
| --------   |
| | Link | ----> We want to know about this mouseout
| |      |   |
| --------   |
| --------   |
| | Link |   |
| |    ----> | but not about this one
| --------   |
--------------
---->: mouse movement

Read this article for understanding better:

www.quirksmode.org - Javascript - Mouse Events

Quirksmode solution


function doSomething(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    if (tg.nodeName != 'DIV') return;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    while (reltg != tg && reltg.nodeName != 'BODY')
        reltg= reltg.parentNode
    if (reltg== tg) return;
    // Mouseout took place when mouse actually left layer
    // Handle event
}

My document


You can find my full document here:

JS Bin - My documents

You can find the original document without mouseEvent(e) function here:

JS Bin - Original

Mouseover on the button then mouse over the box then quickly mouseout and quickly come back to box then it will start to vibrate. (On Firefox 3.6 Windows 7)

Javascript

<script type="text/javascript">
function mouseEvent(e) {
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    if (tg.nodeName != 'DIV') return;
    var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
    while (reltg != tg && reltg.nodeName != 'BODY')
        reltg= reltg.parentNode
    if (reltg== tg) return;
    // Mouseout took place when mouse actually left layer
    // Handle event
}
function toggleByType(id, type, e){
    setTimeout(function(){
        var element = document.getElementById(id);
        if(element.style.display == type){
            mouseEvent(e);
            element.style.display = 'none'; 
        } else {
            element.style.display = type;   
        }
    }, 500);
}
</script>

HTML

<div class="box-container" onmouseover="toggleByType('box','block');" onmouseout="toggleByType('box','block', event);">
    <a href="#" class="box-bridge">Show Box</a>
    <div id="box" class="box" style="display:none;">
            Mouse out and this will dissapear.
            <br />
            <a href="#">Roll over to have problems</a>
        </div>
</div>

My Problem


The solution what quirksmode is giving sounds logical however I don't know how to use the function I tried in a lots of ways what I posted is just one, but I don't get it, so I would be very happy if you could help me to make this work.

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

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

发布评论

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

评论(2

装迷糊 2024-09-23 15:21:52

您在 onmouseover 属性中缺少 event

onmouseover="toggleByType('box','block', event);" 

但是,在菜单显示之前仍有 500 毫秒的延迟。

You're missing event in the onmouseover attribute:

onmouseover="toggleByType('box','block', event);" 

But, you've still a 500ms delay before the menu shows up.

若水微香 2024-09-23 15:21:52

为什么不使用纯CSS?使用 :hover 元标记,您可以轻松设置此菜单,而无需使用任何 JS。适用于所有现代浏览器(IE 7 之前版本除外)。

例子:

.box { display: none }; .box-container:hover .box { display: block }

Why not using pure CSS? With the :hover meta-tag you can easily set-up this menu without using any JS. Works with all modern browsers (except pre-IE 7).

Example:

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