HTML "overlay" which allows clicks to fall through to elements behind it

170,027

Solution 1

A silly hack I did was to set the height of the element to zero but overflow:visible; combining this with pointer-events:none; seems to cover all the bases.

.overlay {
    height:0px;
    overflow:visible;
    pointer-events:none;
    background:none !important;
}

Solution 2

Add pointer-events: none; to the overlay.


Original answer: My suggestion would be that you could capture the click event with the overlay, hide the overlay, then refire the click event, then display the overlay again. I'm not sure if you'd get a flicker effect though.

[Update] Exactly this problem and exactly my solution just appeared in this post: "Forwarding Mouse Events Through Layers". I know its probably a little late for the OP, but for the sake of somebody having this problem in the future, I though I would include it.

Solution 3

For the record an alternative approach might be to make the clickable layer the overlay: you make it semi-transparent and then place the "overlay" image behind it (somewhat counterintuitively, the "overlay" image could then be opaque). Depending on what you're trying to do, you might well be able to get the exact same visual effect (of an image and a clickable layer semi-transparently superimposed on top of each other), while avoiding clickability problems (because the "overlay" is in fact in the background).

Solution 4

In case anyone else is running in to the same problem, the only solution I could find that satisfied me was to have the canvas cover everything and then to raise the Z-index of all clickable elements. You can't draw on them, but at least they are clickable...

Solution 5

My team ran into this issue and resolved it very nicely.

  • add a class "passthrough" or something to each element you want clickable and which is under the overlay.
  • for each ".passthrough" element append a div and position it exactly on top of its parent. add class "element-overlay" to this new div.
  • The ".element-overlay" css should have a high z-index (above the page's overlay), and the elements should be transparent.

This should resolve your problem as the events on the ".element-overlay" should bubble up to ".passthrough". If you still have problems (we did not see any so far) you can play around with the binding.

This is an enhancement to @jvenema's solution.

The nice thing about this is that

  • you don't pass through ALL events to ALL elements. Just the ones you want. (resolved @jvenema's argument)
  • All events will work properly. (hover for example).

If you have any problems please let me know so I can elaborate.

Share:
170,027

Related videos on Youtube

Steven Schlansker
Author by

Steven Schlansker

Updated on July 08, 2022

Comments

  • Steven Schlansker
    Steven Schlansker almost 2 years

    I'm trying to overlay a element on top of a webpage (to draw arbitrary graphics), and I've come to the point where I can stack it inside of a element on top of everything, but this prevents the user from clicking on any links/buttons/etc. Is there a way to have its content float on top of everything (it's semi-transparent, so you can still see what is behind) and have the user interact with the layer below it?

    I've found a lot of information on the DOM event model, but none of it addresses the problem where the buttons and other "native" controls never seem to get the clicks in the first place.

    • kitchin
      kitchin almost 12 years
    • Uday Hiwarale
      Uday Hiwarale over 9 years
      $("div").click(function(e){e.preventDefault();}); I use this most of the times
    • Mario
      Mario about 6 years
      This is something that should be (but hasn't been yet) addressed in the w3c dom spec (rather than with a hack) as it has many potential applications.
  • Wiesław Šoltés
    Wiesław Šoltés over 14 years
    "Taking your scenario, if you had evil intentions, you could hide everything underneath your "overlay". Then, when a user clicks on a link they think should take them to bankofamerica.com, instead it triggers the hidden link which takes them to myevilsite.com." -- That doesn't even make sense. Why would he need to go to the trouble. He could just use js to go there, why would he need to trigger a hidden link?
  • Steven Schlansker
    Steven Schlansker over 14 years
    No evil intentions here! This is strictly in-house; it only really has to work on one of Safari or Firefox on exactly one computer :) I care nothing about portability at the moment. Your idea is interesting, but it requires nesting within the element - which does not work for things like form buttons or links, which is the use case I described in the original post :(
  • Jerod Venema
    Jerod Venema over 14 years
    @Steven, I know, which is why I mentioned that I think you're out of luck on that approach. @Russell, if you consider then that you can have a user perform actions without them knowing it, you can do things like start redirecting keyboard events, etc, which, in turn, would let you enter filenames for upload, and so on. This was a big deal with flash not too long ago, because without knowing it, you could turn on your flash webcam and/or audio controls and send audio/video without realizing it.
  • mbillard
    mbillard over 14 years
    I think that if the overlay is transparent (as in no background, a transparent image as background will not work), clicks will fall through. I'm not sure if this works for all browsers though.
  • Corey Ballou
    Corey Ballou over 14 years
    Correct, as this is how clickjacking occurs (transparent fullscreen iframe sits on top of a page).
  • Nicolas Dumazet
    Nicolas Dumazet about 14 years
    The link you provided is awesome. In particular, it allowed me to learn about Mozilla "pointer-events" property that does exactly the trick. hacks.mozilla.org/2009/12/…
  • maaartinus
    maaartinus almost 10 years
    Interesting, but I guess I don't really understand. However, it brought to another idea: Make the real layer fully transparent and put it above the overlay. Make a non-transparent copy and put it below. All events are guaranteed to work, the display may be wrong if the copy gets out of sync. WDYT?
  • Tom
    Tom almost 9 years
    I like this - works for me as long as the overlay is completely transparent, e.g. if it is just used to position a message/button/image.
  • lepe
    lepe over 8 years
    Basically you need to add pointer-events: none; CSS to the element above so it is "invisible" to the events.
  • briiC
    briiC over 8 years
    crazy easy, thanks! Works on svg elements too.
  • user2867288
    user2867288 about 8 years
    Note: pointer-events: none isn't supported in IE10 and below or Opera Mini. Many users are still using old IE versions.
  • VSO
    VSO about 7 years
    @lepe: <3 All this complications, when all that was really needed was your awesome concise solution.
  • Nathan Arthur
    Nathan Arthur over 6 years
    @user2867288 Many as in about half a percent in the US?
  • Brett L
    Brett L about 3 years
    I can see how this could be used to pass the events from the transparent ".element-overlay" to the ".passthrough" element, which is below the actual overlay, but does the actual overlay also receive those events? I'm trying to find a way to capture the events on the overlay, and again on the lower layer, preferably without nesting the overlay inside the other layers or vice-versa.
  • Mrigank Pawagi
    Mrigank Pawagi over 2 years
    Excellent solution!
  • Déjà vu
    Déjà vu about 2 years
    @NathanArthur 0.14% as of today - the ugly beast is still alive!