我已经直接从Bootstrap示例中使用了我的模态代码,并且只包括Bootstrap .js(而不是Bootstrap -modal.js)。但是,我的模态出现在灰色渐隐(背景)下面,是不可编辑的。

这是它的样子:

这是重现这个问题的一种方法。该代码的基本结构是这样的:

<body>
    <p>Lorem ipsum dolor sit amet.</p>    

    <div class="my-module">
        This container contains the modal code.
        <div class="modal fade">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-body">Modal</div>
                </div>
            </div>
        </div>
    </div>
</body>
body {
    padding-top: 50px;
}

.my-module {
    position: fixed;
    top: 0;
    left: 0;
}

你知道为什么会这样吗或者我能做些什么来弥补吗?


当前回答

我在使用Angular 14时遇到了这个问题,我通过更改.modal- background解决了这个问题,from position: fixed;相对位置:相对位置;

::ng-deep.modal-backdrop {
  --bs-backdrop-zindex: 1050;
  --bs-backdrop-bg: #000;
  --bs-backdrop-opacity: 0.5;
  position: relative;
  top: 0;
  left: 0;
  z-index: var(--bs-backdrop-zindex);
  width: 100vw;
  height: 100vh;
  background-color: var(--bs-backdrop-bg);
}

其他回答

另一种方法是从bootstrap.css中的.modal- background中删除z-index。这将导致背景与身体的其他部分在同一水平线上(它仍然会褪色),你的模态在顶部。

.modal- background是这样的

.modal-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: #000000;
}

我发现:

在bootstrap 3.3.0中这是不正确的,但在bootstrap 3.3.5中这是正确的。让我们看看代码。 3.3.0:

this.$backdrop = $('<div class="modal-backdrop ' + animate + '" />')
    .prependTo(this.$element)

3.3.5

  this.$backdrop = $(document.createElement('div'))
        .addClass('modal-backdrop ' + animate)
        .appendTo(this.$body)

我有一个更轻更好的解决方案。

它可以很容易地解决通过一些额外的CSS样式..

隐藏类.modal- background(从Bootstrap.js自动生成) .modal-backdrop { 显示:没有; 可见性:隐藏; 位置:相对 } 将.modal的背景设置为半透明的黑色背景图像。 .modal { 背景:url(“http://bin.smwcentral.net/u/11361/BlackTransparentBackground.png”) //来自谷歌图像的半透明图像 z - index: 1100; }

如果你有一个需求,需要模态在元素内部,而不是在</body>标签附近,这将是最好的工作。

将这个添加到页面中。 这对我很管用。

<script type="text/javascript">
    $('.modal').parent().on('show.bs.modal', function (e) { $(e.relatedTarget.attributes['data-target'].value).appendTo('body'); })
</script>

将绝对位置更改为相对位置。

.modal-backdrop {
    position: relative;
    /* ... */
}