我已经直接从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;
}

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


当前回答

我尝试了上面提供的所有选项,但没有得到它的工作使用这些。

什么是有效的:设置。modal- background的z-index为-1。

.modal-backdrop {
  z-index: -1;
}

其他回答

@Muhd提供的解决方案是最好的方法。但如果你陷入了无法更改页面结构的情况,可以使用以下技巧:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" data-backdrop="false" style="background-color: rgba(0, 0, 0, 0.5);">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"
                aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
        </div>
        <div class="modal-body">...</div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
    </div>
</div>

这里的技巧是data- background ="false" style="background-color: rgba(0, 0, 0, 0.5);"通过删除默认背景并通过设置对话框本身的背景色来创建一个虚拟背景。

更新1: 正如@Gustyn指出的那样,点击背景并不能像预期的那样关闭对话框。为了实现这个,你必须添加一些java脚本代码。这里有一些例子,你可以实现这一点。

$('.modal').click(function(event){
    $(event.target).modal('hide');
});

如果模态容器具有固定或相对位置,或者位于具有固定或相对位置的元素中,则会发生这种行为。

确保模态容器及其所有父元素都以默认方式定位以解决问题。

这里有一些方法:

最简单的方法是移动modal div,使其位于任何具有特殊定位的元素之外。一个好地方可能就在body结束标签</body>之前。 或者,您可以从模态及其祖先中删除position: CSS属性,直到问题消失。然而,这可能会改变页面的外观和功能。

在你的情态动词中使用:

data-backdrop="false" 

解决办法是把模态直接放在body标签下面。如果由于某些原因这是不可能的,那么只需添加一个overflow: visible属性到包含模态的标记中,或使用以下代码:

<script>
    $(document).on('show.bs.modal', '.modal', function () {
        $('.div-wrapper-for-modal').css('overflow', 'visible');
    });

    $(document).on('hide.bs.modal', '.modal', function () {
        $('.div-wrapper-for-modal').css('overflow', 'auto');
    });
</script>
<div class="div-wrapper-for-modal">
    <div class="modal hide fade">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3>Modal header</h3>
        </div>
        <div class="modal-body">
            <p>One fine body…</p>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn">Close</a>
            <a href="#" class="btn btn-primary">Save changes</a>
        </div>
    </div>
</div>

另外,确保BootStrap的css和js版本是相同的。不同版本也可以使模态出现在背景下:

例如:

Bad:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

好:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>