Javascript:[ onmouseover ] 和 [ onmouseout ] 事件
一般问题
例如,假设有一个下拉菜单,当您将鼠标悬停在
链接上时,会弹出下拉菜单。
但正如您在下面的文章中所读到的那样,它存在问题,当您将鼠标悬停在链接上时(对于某些浏览器,元素内的所有内容),该框都会消失。问题来自于事件冒泡
。
在我的文档中,onmouseover
和 onmouseout
都延迟了 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
}
我的文档
您可以在这里找到我的完整文档:
您可以在这里找到不带 mouseEvent(e)
函数的原始文档:
将鼠标悬停在按钮上,然后将鼠标悬停在框上,然后快速将鼠标移出并快速返回到然后盒子就会开始振动。 (在 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:
You can find the original document without mouseEvent(e)
function here:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
onmouseover
属性中缺少event
:但是,在菜单显示之前仍有 500 毫秒的延迟。
You're missing
event
in theonmouseover
attribute:But, you've still a 500ms delay before the menu shows up.
为什么不使用纯CSS?使用 :hover 元标记,您可以轻松设置此菜单,而无需使用任何 JS。适用于所有现代浏览器(IE 7 之前版本除外)。
例子:
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: