[英]Check if another event is fired from an event handler function
Let's imagine I have an event handler function attached to an image, for example, an onmouseover
handler.假设我有一个附加到图像的事件处理程序函数,例如,
onmouseover
处理程序。 I don't want this handler run if an onmouseover
event is fired by a particular element.如果特定元素触发了
onmouseover
事件,我不希望此处理程序运行。
To be more specific, I have an image which being hovered, a menu is popped out.更具体地说,我有一个悬停的图像,弹出一个菜单。 I want to close that menu if the mouse moves out of that image, unless I move the mouse to the menu, which is adjacent to the image.
如果鼠标移出该图像,我想关闭该菜单,除非我将鼠标移到与该图像相邻的菜单。
So something like this in pseudo code:所以在伪代码中是这样的:
img.mouseout = function () {
if (otherelement.onmouseover.fired) {
leave the menu as it is
}
else
{
close the menu
}
So how can I check whether another event was fired?那么如何检查是否触发了另一个事件?
I would add a timeout and a mouseover handler that clears it in the other handler.我会添加一个超时和一个鼠标悬停处理程序,在另一个处理程序中清除它。 While the elements could be exactly next to each other visually, adding a small timer is more safe since there might just be a tiny 1px area where the mouseout event fires, causing an ugly flickering.
虽然元素可以在视觉上完全相邻,但添加一个小计时器更安全,因为可能只有一个 1px 的微小区域触发 mouseout 事件,从而导致难看的闪烁。
This also allows a quick mouseshake without flicker.这也允许快速鼠标摇动而不会闪烁。
Something like:就像是:
var timer;
img.onmouseout = other.onmouseout = function() {
timer = setTimeout(closemenu, 100);
}
img.onmouseover = other.onmouseover = function() {
clearTimeout(timer);
}
function closemenu() {
// close it
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.