1

I have an square image depicting a car on a transparent background. I want to be able to click on car, yet when I click on it's transparent background I'd like the click to go through the image and trigger on the underlying DOM element (or, specifically, other image).

Can that be achieved somehow?

oldhomemovie
  • 14,621
  • 13
  • 64
  • 99

3 Answers3

3

That's quite complex, but can be achieved by combining Pointer events to allow for the click to bubble up the DOM (reaching the img parent container), and a faux Canvas on top to detect a transparent pixel (which will be used as a flag to activate the event delegation).

Pointer events were derived from the SVG model to allow for event delegation across layers. Note that they're currently not supported on IE and Opera.

Related Posts:

Demo:

Reference:

Further Read:

Community
  • 1
  • 1
Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
0

No, click events on images are not controlled by the transparency of the image. The click will always be triggered on the image, then bubble up to the parent if it's not stopped.

An image can have semi transparent pixels, and if a pixel has for example 61% opacity, then 37% of the click would go to the parent element, and that is of course not possible.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

You could draw nan SVG shape around the car and put the picture inside it, then listen for click events on the shape.

Spadar Shut
  • 15,111
  • 5
  • 47
  • 54