简体   繁体   English

如何在光标下的所有元素上调用鼠标悬停?

[英]How do I get mouseover called on all elements under the cursor?

I have a web application where every time you click, a dot is created (see below). 我有一个Web应用程序,每次单击时,都会创建一个点(见下文)。 When I mouse over a stack of dots, I expected to have every dot under the cursor have its mouseover or mouseenter event fire. 当我将鼠标悬停在一堆点上时,我希望光标下的每个点都有鼠标悬停或鼠标中心事件触发。 However, only one event is fired, that for the dot "on top" of the stack. 但是,只有一个事件被触发,对于堆栈的“顶部”点。

How would you get the mouseover or mouseenter event to fire for every dot under the cursor (but not those that aren't under the cursor) when the mouse is moved over a stack of multiple dots? 当鼠标移动到多个点的堆栈上时,如何为光标下的每个点(但不是光标下的点)触发mouseover或mouseenter事件? (BEGIN EDIT) Additionally, the mouseout or mouseleave event should only fire when the cursor actually leaves the dot. (BEGIN EDIT)此外,mouseout或mouseleave事件只应在光标实际离开点时触发。 (END EDIT) (结束编辑)

In case it's important, the whole black portion in the picture is a SVG, and the dots are all SVG circles. 如果重要的话,图片中的整个黑色部分是SVG,点都是SVG圆圈。 Each dot is a sibling with all the other dots, and all are children of the SVG element. 每个点都是包含所有其他点的兄弟,并且都是SVG元素的子元素。

带点显示的web应用程序

Thanks in advance! 提前致谢!

Evidently, the event is being dispatched only to the event handler registered for the topmost "dot" in the stack. 显然,事件只被分派给为堆栈中最顶端的“点”注册的事件处理程序。 Expand this event handler to programmatically search all other dots and determine whether they may overlap the current position of the sprite. 展开此事件处理程序以编程方式搜索所有其他点,并确定它们是否可能与精灵的当前位置重叠。 If so, mechanically invoke their event handlers individually--specifying a flag so that the "programmatic search [of] all other dots" doesn't happen FOR THEM also, which will never end. 如果是这样,机械地单独调用它们的事件处理程序 - 指定一个标志,以便“所有其他点的程序化搜索”也不会发生,对于它们也永远不会结束。 If you're unable to add a flag to the event handler, add some field to some object that the event handler can observe (eg, the data structure that corresponds to a dot), and set or clear that flag (a good name might be "isCheckForOverlappingDots") beforehand. 如果您无法向事件处理程序添加标志,请向事件处理程序可以观察到的某个对象添加一些字段(例如,对应于点的数据结构),并设置或清除该标志(可能是一个好名称)事先是“isCheckForOverlappingDots”)。

Once you have the first dot, set its CSS "pointer-events" property to "none" and then call document.elementFromPoint using the co-ordinates you have. 获得第一个点后,将其CSS“pointer-events”属性设置为“none”,然后使用您拥有的坐标调用document.elementFromPoint Repeat till you run out of dots. 重复,直到你用完点。

You may need to suppress the mouse handler while you're doing this so you don't get unwanted mouseout/mouseenter events as the elements under the mouse pointer change. 您可能需要在执行此操作时禁止鼠标处理程序,以便在鼠标指针下的元素更改时不会出现不需要的mouseout / mouseenter事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM