我已经直接从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;
}
你知道为什么会这样吗或者我能做些什么来弥补吗?
@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">×</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');
});
在附加到主体时要小心!
这肯定会从背景后面得到那个模态:
$('#myModal').appendTo("body").modal('show');
然而,这将在每次打开主体时添加另一个模态,这可能会导致在更新面板中添加具有编辑和更新的网格视图等控件到模态时头痛。因此,你可以考虑在模式关闭时删除它:
例如:
$(document).on('hidden.bs.modal', function () {
$('.modal').remove();
});
将删除添加的模态。(当然,这是一个例子,你可能想要使用另一个你添加到模态的类名)。
正如其他人已经说过的;你可以简单地从模式中关闭back-drop (data- background ="false"),或者如果你不能没有屏幕变暗,你可以调整.modal- backstage CSS类(或者增加z-index)。
解决办法是把模态直接放在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">×</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>