views:

719

answers:

2

I'm attempting to capture mouse events on an element with another absolutely-positioned element on top of it. Right now, events on the absolutely-positioned element hit it and bubble up to its parent, but I want it to be "transparent" to these mouse events and forward them on to whatever is behind it. How should I implement this?

Click here for an example page. All its divs flash on mousedown.

A: 

The reason you are not receiving the event is because the absolutely positioned element is not a child of the element you are wanting to "click" (blue div). The cleanest way I can think of is to put the absolute element as a child of the one you want clicked, but I'm assuming you can't do that or you wouldn't have posted this question here :)

Another option would be to register a click event handler for the absolute element and call the click handler for the blue div, causing them both to flash.

Due to the way events bubble up through the DOM I'm not sure there is a simpler answer for you, but I'm very curious if anyone else has any tricks I don't know about!

Loktar
Thanks for the response, Loktar. Unfortunately, on the real page I'm not going to know what's underneath the absolutely-positioned element, and there may be more than one possible target. The only workaround I can think of is grabbing the mouse coordinates and comparing them against possible targets to see if they intersect. That would be just plain nasty.
Sidnicious
+2  A: 

If all you need is mousedown, you may be able to make due with the document.elementFromPoint method, by:

  1. removing the top layer on mousedown,
  2. passing the x and y coordinates from the event to the document.elementFromPoint method to get the element underneath, and then
  3. restoring the top layer.
Jed Schmidt