引导模式对话框可以覆盖另一个对话框吗?
<button class="btn" onClick="$('#firstModal').modal('show');">First</button>
<!-- Modal -->
<div id="firstModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<button class="btn" onClick="$('#secondModal').modal('show');">Second</button>
</div>
</div>
<!-- Modal -->
<div id="secondModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
Some error message goes here.
</div>
</div>
一切工作正常;唯一的问题是第一个对话框显示在第二个对话框的叠加层上。我怎样才能解决这个问题?引导模式对话框可以覆盖另一个对话框吗?
我发现我可以改变使用这个CSS规则的第二个“影子”的z索引:
.modal-backdrop.fade.in:nth-child(2){
z-index: 1060 !important;
}
比我只需要设置第二个对话框的Z-指数例如1070,就是这样,尽管我不确定是否与旧版浏览器兼容。
你可以添加一个属性 “secondModal”
style="z-index:100000;"
不幸的是,这只影响窗口,但不影响背景 – 2013-05-14 16:01:07
使用JavaScript,您可以获得公共父级(包装整个对话框)并在显示对话框后通过脚本添加属性。 – 2013-05-14 16:07:47
@dadlyDev你可以添加一些代码吗?因为我不太明白你的意思。谢谢 – 2013-05-14 16:37:50
我想模态背景(叠加)由BODY中的所有模态共享。
但是,你可以使用“显示”事件从第二模改变对第一模式的不透明度 - 给覆盖的在第一模态的影响:
$('#myModal2').on('show', function() {
$('#myModal').css('opacity', .5);
});
编辑:
如果要禁用模式1,而模式2是开放的,模式2打开时,你可以取消绑定模式1,当模式1关闭时恢复模态1。 我更新了这个。
$('#myModal2').on('show', function() {
$('#myModal').css('opacity', .5);
$('#myModal').unbind();
});
$('#myModal2').on('hidden', function() {
$('#myModal').css('opacity', 1);
$('#myModal').removeData("modal").modal({});
});
这不是最好的解决方案,因为用户仍然可以在显示第二个对话框时点击第一个对话框中的按钮,但无论如何感谢回复 – 2013-05-14 20:02:19
如果我能够为每个对话框设置Z-index背景(叠加) ,问题就解决了 – 2013-05-14 20:05:00
我认为这会很困难。您必须创建自己的自定义叠加/背景并禁用Bootstrap模式背景。 – ZimSystem 2013-05-14 20:22:47
要使覆盖层打开其他覆盖层,我在事件监听器中添加了一个简单的JS行。
这样的覆盖内的任何链接将首先关闭覆盖它是在
搜索在引导覆盖代码:
$(document).on('click.bs.modal.data-api', '[data-toggle="modal"]', function (e) {
添加到该函数:
$this.closest(".modal").modal('hide');
以防万一有人绊倒了这一点。
-
在文档准备功能将此值添加到您的主脚本标记你的页面
var current_zindex = 1049; var current_zindex_bg = 1048;
-
上添加
$('.modal').on('show', function() { current_zindex++;//increment z-index for your modals current_zindex_bg++;//increment z-index for your modal-backdrops $(this).css('z-index',current_zindex); //setting the modal backdrop });
-
现在在minifield引导文件本身找到这一位代码:
'<div class="modal-backdrop '+r+'" />' //just search for "modal-backdrop" without the quotes change to: '<div class="modal-backdrop '+r+'" style="z-index:'+current_zindex_bg+';" />'
就是这样。您可以通过搜索“模态背景”并按照相同的流程在未定义的boostrap.js中找到相同的代码。
点击最顶部的背景会隐藏最想要的模式。
.on(“show.bs.modal”,... \t该事件在调用show实例方法时立即触发。由点击引起的点击元素可用作事件的relatedTarget属性 – panchicore 2015-11-07 00:56:50
我写了一篇博客文章中有关如何解决这个堆放问题编程:http://gurde.com/stacked-bootstrap-modals/
$(document)
.on('show.bs.modal', '.modal', function(event) {
$(this).appendTo($('body'));
})
.on('shown.bs.modal', '.modal.in', function(event) {
setModalsAndBackdropsOrder();
})
.on('hidden.bs.modal', '.modal', function(event) {
setModalsAndBackdropsOrder();
});
function setModalsAndBackdropsOrder() {
var modalZIndex = 1040;
$('.modal.in').each(function(index) {
var $modal = $(this);
modalZIndex++;
$modal.css('zIndex', modalZIndex);
$modal.next('.modal-backdrop.in').addClass('hidden').css('zIndex', modalZIndex - 1);
});
$('.modal.in:visible:last').focus().next('.modal-backdrop.in').removeClass('hidden');
}
我还没有测试过下面的答案,但觉得由于它没有被后向浏览器兼容,所以与我现在需要的不匹配。说,我已经投票赞成这个答案,因为它为我提供了一些解决方案,并进行了一些调整......我发现使用这个脚本时会出现一个奇怪的闪烁,用最新的引导程序来摆脱这个问题,删除或删除第18行的“.addClass('hidden')”并注释掉或删除第20行,现在所有作品都很性感。感谢罗伯特为此。 – NinjaKC 2015-06-22 03:36:19
比方说,你有模态var modal = $('.some-selector').modal('show');
首先要改变它的zIndex的,如:modal.css({zIndex: 7760})
你可以找到参考其在modal.data()
中的$背景,以便您可以访问它并更改任何内容:modal.data()['bs.modal'].$backdrop.css({zIndex: 7750});
您可以简单地做t他的方式:(注意:您需要引导3本)
<button class="btn" data-target="#firstModal" data-toggle="modal">First</button>
<div id="firstModal" data-target="#secondModal" data-dismiss="modal"
data-toggle="modal">
<div class="modal-body">
<button class="btn" onClick="$('#secondModal').modal('show');">Second</button>
</div>
</div>
<div id="secondModal" >
<div class="modal-body">
Some error message goes here.
</div>
</div>
一个简单的实现它:HTTP://sforsuresh.in/bootstrap-modal-window-close - 当前打开的新模式/ – 2018-03-06 11:38:17