How can I get a child of an element with lower z-index on front of a child of another element with higher z-index?

前端 未结 5 992
甜味超标
甜味超标 2021-01-20 01:27

I have the following setup:

Whatever
<
相关标签:
5条回答
  • 2021-01-20 01:56

    Added colour to the div's to demonstrate the layering:

    <div style="position:absolute;background:blue;z-index: 10">
          <div>Whatever</div>
    </div>
    
    <div style="position:absolute;background:red;z-index: 11">
          <div><div>Haaaleluia</div></div>
    </div>
    
    0 讨论(0)
  • 2021-01-20 01:58

    In short: you can't. It's not possible to have a child of a parent element with a z-index lower than a sibling, and give that child a higher z-index value than the parent's sibling, as the inherited z-index is relative to the parent (see this question for someone coming up against the same thing).

    However, depending on your setup, you can remove the z-indices completely, and let the browser place your top div above the one below. You could then play around with giving just the children a z-index value.

    0 讨论(0)
  • 2021-01-20 02:04

    This is just an idea which may work.

    1. Get the div having z-index = 9 using jquery.
    2. Then select the 1st child of 1st child of the div having z-index as 9.
    3. Then apply the style as follows:

    $(element which you got).attr("style", "position: absolute; z-index: 12;")

    The style will be applied to the small element and it will be visible on the red box.

    0 讨论(0)
  • 2021-01-20 02:11

    Without changing the locations, z-index, or rearranging any of the elements, the only way I can think of that would allow the div underneath to appear would be to either change the visibility, display or opacity of the div overlapping the one you want to see.

    That is, use one of the following CSS styles on the parent div of "Whatever":

    visibility: hidden;
    display: none;
    opacity: 0;
    

    You're basically asking to display an element above another element that has a higher z-index, which defeats the purpose of having a z-index. If you're trying to control how elements overlap, it really should be done with z-index. I would suggest you rethink how your elements are organized (maybe move the "Haaaleluia" div outside of its parent and assign it its own z-index). Otherwise, I might suggest you consider creating a duplicate of "Haaaleluia" to display above "Whatever", which may or may not suit your situation.

    Edit: Looking at the image you've provided, changing the parent of the order box to the parent of the tutorial div may work for you. Here is a demo using jQuery that may help illustrate the point - just change the hover event to whatever it is that starts the tutorial. Another option may be to clone the order box to lay on top of the one below, but with a higher z-index (so that the user only sees one, but the clone overlaps everything else). This is assuming that your map div is positioned either absolutely or relatively. Hope that helps.

    0 讨论(0)
  • 2021-01-20 02:19

    Nice design.

    Ok so I'll first say that I believe the solution to your layering problem, as others have already suggested, is moving that box outside of its parent (the map).

    But you set some constraints, so I'll try to break as few as possible. I don't know how to do this without breaking any of your constraints. Z-index is inherited (again, others have pointed this out), so you can't break out of your parents' layer with only that tool.

    So here's one way you could get the same effect, using javascript. It's ugly, and might cause you more headaches later, but it should at least work:

    1. Find out the absolute position of the div that you want to put on top.
    2. Make a copy of it.
    3. Hide the original (optional if it's opaque).
    4. Insert the copy on top of everything.

    If you're using jQuery, you can get elements' position relative to the document with the .offset() function. After that it's fairly simple:

    $(document).ready( function() {
    
        $("a[href='#overlay']").click( function() {
            // 1: get the position
            pos = $('.wrap').offset();
            // 2: make a copy
            halecopy = $('.bottom .wrap').clone();
            // 3: hide the original
            $('.bottom .wrap').css({opacity: 0});
            // 4: Insert new label on top of everything
            $('body').prepend(halecopy);
            // position the new label correctly
            halecopy.css({position:'absolute',
                          top: pos.top,
                          left: pos.left,
                          'z-index': 2});
            // show the "top" layer
            $('.top').fadeIn();
        });
    
        $("a[href='#hide']").click( function() {
            $('.top').fadeOut( function() {
                // remove the label copy
                halecopy.remove();
                // show the original again
                $('.bottom .wrap').css({opacity: 1});
            });
        });
    
    });​
    

    That script works for me on this markup:

    <div class="top">
        <div class="label">
            <p>Whatever</p>
        </div>
    </div>
    
    <div class="bottom">
        <div class="wrap">
            <div class="label">
                <p>Haaaleluuuuia!</p>
            </div>
        </div>
    </div>
    
    <a href="#overlay">show</a>
    <a href="#hide">hide</a>​
    

    With these styles:

    .top,
    .bottom {
        position: absolute;
        top: 10%;
        left: 3%;
    }
    
    .top {
        z-index: 1;
        padding: 2%;
        background: rgba(0, 127, 127, 0.8);
        display:none;
    }
    
    .bottom {
        z-index: -1;
        padding: 3%;
        background: rgba(127, 127, 0, 0.8);
    }
    
    .label {
        color: #fff;
    }
    
    .wrap {
        outline: 1px dashed rgba(127, 0, 0, 0.8);
        background: rgba(127, 127, 127, 0.8);
    }
    
    .bottom .label {
        z-index: 10;
    }
    ​
    

    And here's a handly jsfiddle demo.

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