Click through div to underlying elements

后端 未结 16 2084
逝去的感伤
逝去的感伤 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:28

    Another idea to try (situationally) would be to:

    1. Put the content you want in a div;
    2. Put the non-clicking overlay over the entire page with a z-index higher,
    3. make another cropped copy of the original div
    4. overlay and abs position the copy div in the same place as the original content you want to be clickable with an even higher z-index?

    Any thoughts?

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

    Yes, you CAN do this.

    Using pointer-events: none along with CSS conditional statements for IE11 (does not work in IE10 or below), you can get a cross browser compatible solution for this problem.

    Using AlphaImageLoader, you can even put transparent .PNG/.GIFs in the overlay div and have clicks flow through to elements underneath.

    CSS:

    pointer-events: none;
    background: url('your_transparent.png');
    

    IE11 conditional:

    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='your_transparent.png', sizingMethod='scale');
    background: none !important;
    

    Here is a basic example page with all the code.

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

    I'm adding this answer because I didn’t see it here in full. I was able to do this using elementFromPoint. So basically:

    • attach a click to the div you want to be clicked through
    • hide it
    • determine what element the pointer is on
    • fire the click on the element there.
    var range-selector= $("")
        .css("position", "absolute").addClass("range-selector")
        .appendTo("")
        .click(function(e) {
            _range-selector.hide();
    
            $(document.elementFromPoint(e.clientX,e.clientY)).trigger("click");
        });
    

    In my case the overlaying div is absolutely positioned—I am not sure if this makes a difference. This works on IE8/9, Safari Chrome and Firefox at least.

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

    An easier way would be to inline the transparent background image using Data URIs as follows:

    .click-through {
        pointer-events: none;
        background: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7);
    }
    
    0 讨论(0)
  • 2020-11-21 06:32

    I think that you can consider changing your markup. If I am not wrong, you'd like to put an invisible layer above the document and your invisible markup may be preceding your document image (is this correct?).

    Instead, I propose that you put the invisible right after the document image but changing the position to absolute.

    Notice that you need a parent element to have position: relative and then you will be able to use this idea. Otherwise your absolute layer will be placed just in the top left corner.

    An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is html

    Hope this helps. See here for more information about CSS positioning.

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

    I needed to do this and decided to take this route:

    $('.overlay').click(function(e){
        var left = $(window).scrollLeft();
        var top = $(window).scrollTop();
    
        //hide the overlay for now so the document can find the underlying elements
        $(this).css('display','none');
        //use the current scroll position to deduct from the click position
        $(document.elementFromPoint(e.pageX-left, e.pageY-top)).click();
        //show the overlay again
        $(this).css('display','block');
    });
    
    0 讨论(0)
提交回复
热议问题