Click through div to underlying elements

后端 未结 16 2085
逝去的感伤
逝去的感伤 2020-11-21 06:19

I have a div that has background:transparent, along with border. Underneath this div, I have more elements.

Curre

相关标签:
16条回答
  • 2020-11-21 06:47
    1. Hide overlaying the element
    2. Determine cursor coordinates
    3. Get element on those coordinates
    4. Trigger click on element
    5. Show overlaying element again
    $('#elementontop').click(e => {
        $('#elementontop').hide();
        $(document.elementFromPoint(e.clientX, e.clientY)).trigger("click");
        $('#elementontop').show();
    });
    
    0 讨论(0)
  • 2020-11-21 06:51

    Nope, you can't click ‘through’ an element. You can get the co-ordinates of the click and try to work out what element was underneath the clicked element, but this is really tedious for browsers that don't have document.elementFromPoint. Then you still have to emulate the default action of clicking, which isn't necessarily trivial depending on what elements you have under there.

    Since you've got a fully-transparent window area, you'll probably be better off implementing it as separate border elements around the outside, leaving the centre area free of obstruction so you can really just click straight through.

    0 讨论(0)
  • 2020-11-21 06:53

    I think the event.stopPropagation(); should be mentioned here as well. Add this to the Click function of your button.

    Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

    0 讨论(0)
  • 2020-11-21 06:54

    This is not a precise answer for the question but may help in finding a workaround for it.

    I had an image I was hiding on page load and displaying when waiting on an AJAX call then hiding again however...

    I found the only way to display my image when loading the page then make it disappear and be able to click things where the image was located before hiding it was to put the image into a DIV, make the size of the DIV 10x10 pixels or small enough to prevent it causing an issue then hiding the containing div. This allowed the image to overflow the div while visible and when the div was hidden, only the divs area was affected by inability to click objects beneath and not the whole size of the image the DIV contained and was displaying.

    I tried all the methods to hide the image including CSS display=none/block, opacity=0, hiding the image with hidden=true. All of them resulted in my image being hidden but the area where it was displayed to act like there was a cover over the stuff underneath so clicks and so on wouldn't act on the underlying objects. Once the image was inside a tiny DIV and I hid the tiny DIV, the entire area occupied by the image was clear and only the tiny area under the DIV I hid was affected but as I made it small enough (10x10 pixels), the issue was fixed (sort of).

    I found this to be a dirty workaround for what should be a simple issue but I was not able to find any way to hide the object in its native format without a container. My object was in the form of etc. If anyone has a better way, please let me know.

    0 讨论(0)
提交回复
热议问题