简体   繁体   English

检查是否从事件处理函数触发了另一个事件

[英]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.

相关问题 将事件对象从事件处理程序传递到另一个函数 - Passing around event object from event handler to some another function 检查哪个事件使用jQuery触发了该函数? - Check which event fired the function with jQuery? 从另一个whithreact触发事件处理程序并减少事件时,将div附加到组件上 - append div on a component when an event handler is fired from another one whithreact and reduce 如何检查事件是否被触发? - How to check if an event was fired or not? 添加事件侦听器时触发的事件处理程序 - Event handler fired when add a event listener 如何在触发另一个事件后触发事件函数? - How to fire an event function only after another event is fired? 有没有办法将事件处理函数调用到Meteor中的另一个事件处理程序? - Is there a way to call an event handler function to another event handler in Meteor? 通过事件处理程序将参数从一个函数发送到另一个函数 - sending arguments from one function to another through event handler 删除另一个函数添加的事件处理程序函数 - Remove event handler function added by another function 存储一个事件处理程序中的变量以供另一事件处理程序使用 - Store variable from one event handler to be used by another event handler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM