简体   繁体   English

如何在Javascript中“抛出” mouseEvent?

[英]How to “throw” mouseEvent in Javascript?

I have an SVG with overlapping elements, absolutely positioned. 我有一个SVG,元素重叠,位置绝对正确。 The elements are contained in layers (using the SVG g ) tag, and so may not always have a common parent. 元素包含在图层中(使用SVG g )标签,因此可能并不总是具有公共父元素。

In D3, elements can respond to click events that they catch. 在D3中,元素可以响应其捕获的单击事件。 However, overlapping elements prevent the bottom element from catching any click events. 但是,重叠元素会阻止底部元素捕获任何单击事件。 (Due to the design of my application, I can't change their DOM order arbitrarily - it matters.) I'd like to know if there's a way to pass the click event through an element while preserving drag events - almost like "throwing" the event, if possible. (由于我的应用程序的设计,我不能任意更改其DOM顺序-这很重要。)我想知道是否有一种方法可以在保留拖动事件的同时将click事件传递给元素-几乎就像“ throwing如果可能的话。

Due to the fact that I still need drag events and other pointer events, I can't use the "pointer-events: none" property. 由于我仍然需要拖动事件和其他指针事件,因此不能使用“ pointer-events:none”属性。 Additionally, since these elements may not have a common ancestor, I can't really loop through elements in that manner. 另外,由于这些元素可能没有共同的祖先,因此我无法真正以这种方式遍历元素。

My desired behavior is a toggle - clicking on a shape will call the shape's click handler, and select that shape. 我想要的行为是切换-单击形状会调用该形状的点击处理程序,然后选择该形状。 Clicking a selected shape (if it overlaps another shape) will call the selected shape's click handler, which will "throw" the click event so that it will pass through to the underlying element. 单击选定形状(如果它与另一个形状重叠)将调用选定形状的单击处理程序,该处理程序将“引发” click事件,以便其传递到基础元素。 (Maybe using something like, say, d3.event.throw or, like, return d3.event , or an idea like that). (也许使用诸如d3.event.throw类的东西,或者诸如return d3.event的想法)。

Is this possible? 这可能吗?

The click event is handled by the element's click event handler. click事件由元素的click事件处理程序处理。 So everything you would like to do, is to call teh underlying element's click event handler. 因此,您要做的所有事情就是调用底层元素的click事件处理程序。 But if you also wanna pass the click coordinates of the overlying element to the underlying element's click event handler, you have to pass them (coordinates)/it (event) evidente: 但是,如果您还想将上层元素的点击坐标传递给基础元素的click事件处理程序,则必须传递它们(坐标)/它(事件)证据:

overlyingElement.onclick=function(e){
    var event=e||window.event;
    underlyingElement.click(event);
}

Never tested, but 从未测试,但是

I hope it will work okay and in IE too. 我希望它也能在IE中正常运行。

Does that mean that you want to select all the elements under this element on the click? 这是否意味着您要在单击时选择此元素下的所有元素? I am not sure why you would want to do that, but then this is what you are looking for: http://www.vinylfox.com/forwarding-mouse-events-through-layers/ 我不确定为什么要这么做,但这就是您要寻找的: http : //www.vinylfox.com/forwarding-mouse-events-through-layers/

The basic idea is: 基本思想是:

  1. Receive mouseevent 接收mouseevent
  2. Temporarily set display: none on the present element. 临时设置display: none当前元素上display: none
  3. Determine which element lies under the present element using document.elementFromPoint , which works in Firefox , IE , and WebKit as well now. 使用document.elementFromPoint确定当前元素下的哪个元素,该元素现在也可以在FirefoxIE和WebKit中使用。
  4. Set display back to what it was on the element. display设置回原样。
  5. dispatch the event on the element under it. 将事件dispatch到其下的元素。
  6. Make sure that this does not lead to infinite loops by setting flags on the event or other global flags. 通过在事件或其他全局标志上设置标志,确保不会导致无限循环

This works for one level of depth because elementFromPoint would return the top level element after the second element tries to do the same thing. 这适用于一个深度级别,因为elementFromPoint将在第二个元素尝试执行相同操作后返回顶级元素。 If you want to go deeper in the element stack, then you can actually set display property back after all the lower elements have received the event via callbacks set on the event you forward or some other global mechanism. 如果您想深入了解元素堆栈,则可以在所有较低的元素都通过转发event或其他全局机制设置的回调收到所有事件之后,将display属性实际设置回去。

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

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