Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling click events on z-index'd layers

I have 2 z-index layers in a map application I'm building. I have a problem when I click on the layers to zoom in. The click handler is on the underlying z-index layer and I don't want it to fire when a control in the overlying layer is clicked.

The problem i have is that the event gets raised no matter what but the originalTarget property of the event is not the image in the underlying layer when something on the top layer is clicked. Is there anyway to change this?

like image 551
Alex Avatar asked Jun 22 '09 08:06

Alex


2 Answers

It's called event-bubbling, and you can control it with the event.stopPropagation() method (event.cancelBubble() in IE). You can also control it by returning true/false from handlers called by onwhatever attributes on elements. It's a tricky subject so I suggest you do some research.

Info: cancelBubble, stopPropagation

like image 162
SpliFF Avatar answered Sep 23 '22 08:09

SpliFF


Although this does not address the problem directly it may be a viable workaround until a more fitting solution presents itself.

Write a single function to be called onClick and allow the function enough smarts to know who called it. The function then takes the appropriate action based upon who clicked it. You could send it pretty much anything that would be unique and then use a switch.

simplified example :

<html>
<body>

<script type="text/javascript">
function myClickHandle(anID)
{
switch(anID){
case 'bottom': 
      alert("I am on the bottom");
      break;
case 'top':
      alert("I am on the top");
      break;
}
}
</script>

<html>
<div style="z-index:10"><input type=button value='top' onclick="myClickHandle(this.value)"/></div>
<div style="z-index:11"><input type=button value='bottom' onclick="myClickHandle(this.value)"/></div>
</body>
</html>
like image 20
Cranium Slows Avatar answered Sep 19 '22 08:09

Cranium Slows