Exposing element outside modal-backdrop in CSS Boo

2019-05-16 23:33发布

问题:

I'm using CSS Bootstrap's Modal feature, and it's working perfectly. However, I want to add a functionality that while the modal dialog is open and the rest of the web page is covered up by the .modal-backdrop, one of the outside elements from a different place within the scope of the page's structure can be exposed on top of the backdrop:

<div id="outside-element">
  I want this element exposed even while the modal is active,
  upon clicking the button in the modal dialog box
</div>

<div id="help-box" class="modal fade" data-backdrop="static" data-keyboard="false" data-show="true" tabindex="-1" role="dialog" aria-labelledby="help-label" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
              Click this button to show the outside element:
              <button type="button" class="btn" aria-hidden="true" id="test-item">Click it!</button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
  (function($) {
     $('#test-item').click(function() {
        $('#outside-element').attr('style','zIndex:1041');
     });
  })(jQuery);
</script>

As you can see in my current attempt above, I'm setting the z-index of the outside element to 1041, which is one higher than the .modal-backdrop setting (1040). This is not accomplishing what I want it to (namely, placing the #outside-element on top of the modal backdrop), even though the code is sound and "working" (that is, it's definitively modifying the z-index).

回答1:

It would be great if you have a fiddle to show your code in a way that we can run it and see what's going on.

You will need to include the position of the element, for a z-index to take effect:

#outside-element {
   position: relative;
   z-index: 1041;
}


回答2:

The .modal-backdrop has a z-index of 1040, but the .modal itself (#help-box) has a z-index of 1050, so if your outside element needs to be in front of the backdrop and in front of the dialog, you'll have to give it a z-index of 1051 rather than 1041.

If it only needs to be in front of the backdrop, but will not be overlapping with the dialog, then a z-index of 1041 should work, but, as @uapuap mentioned, you'll also need to give it a position, like position: relative.

Here's a fiddle showing it working, with the outside element in front of the backdrop only, with a z-index of 1041 and position: relative:

http://jsfiddle.net/fgyja20w/