简体   繁体   English

多模态叠加

[英]Multiple modals overlay

I need that the overlay shows above the first modal, not in the back.我需要叠加层显示在第一个模态上方,而不是在后面。

模态叠加在后面

 $('#openBtn').click(function(){ $('#myModal').modal({show:true}) });
 <a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a> <div class="modal" id="myModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Modal title</h4> </div><div class="container"></div> <div class="modal-body"> Content for the dialog / modal goes here. <br> <br> <br> <br> <br> <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <div class="modal" id="myModal2" data-backdrop="static"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Second Modal title</h4> </div><div class="container"></div> <div class="modal-body"> Content for the dialog / modal goes here. </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/js/bootstrap.min.js"></script>

I tried to change the z-index of .modal-backdrop , but it becomes a mess.我试图更改.modal-backdropz-index ,但它变得一团糟。

In some cases I have more than two modals on the same page.在某些情况下,我在同一页面上有两个以上的模式。

After seeing many fixes for this, and none of them were exactly what I needed I've came up with a even shorter solution that is inspired by @YermoLamers & @Ketwaroo.在看到许多针对此问题的修复程序,但没有一个是我所需要的,我想出了一个更短的解决方案,其灵感来自@YermoLamers和 @Ketwaroo。

Backdrop z-index fix背景 z-index 修复
This solution uses a setTimeout because the .modal-backdrop isn't created when the event show.bs.modal is triggered.此解决方案使用setTimeout因为在触发事件show.bs.modal时不会创建.modal-backdrop

$(document).on('show.bs.modal', '.modal', function () {
    var zIndex = 1040 + (10 * $('.modal:visible').length);
    $(this).css('z-index', zIndex);
    setTimeout(function() {
        $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
    }, 0);
});
  • This works for every .modal created on the page (even dynamic modals)这适用于.modal创建的每个.modal (甚至是动态模态)
  • The backdrop instantly overlays the previous modal背景立即覆盖了之前的模态

Example jsfiddle示例 jsfiddle

If you don't like the hardcoded z-index for any reason you can calculate the highest z-index on the page like this:如果您出于任何原因不喜欢硬编码的 z-index,您可以像这样计算页面上的最高 z-index

var zIndex = Math.max.apply(null, Array.prototype.map.call(document.querySelectorAll('*'), function(el) {
  return +el.style.zIndex;
})) + 10;

Scrollbar fix滚动条修复
If you have a modal on your page that exceeds the browser height, then you can't scroll in it when closing an second modal.如果页面上的模态超过浏览器高度,则在关闭第二个模态时将无法在其中滚动。 To fix this add:要解决此问题,请添加:

$(document).on('hidden.bs.modal', '.modal', function () {
    $('.modal:visible').length && $(document.body).addClass('modal-open');
});

Versions版本
This solution is tested with bootstrap 3.1.0 - 3.3.5此解决方案已使用 bootstrap 3.1.0 - 3.3.5 进行测试

I realize an answer has been accepted, but I strongly suggest not hacking bootstrap to fix this.我意识到一个答案已被接受,但我强烈建议不要破解引导程序来解决这个问题。

You can pretty easily achieve the same effect by hooking the shown.bs.modal and hidden.bs.modal event handlers and adjusting the z-index there.你可以很容易地通过挂钩showed.bs.modal 和hidden.bs.modal 事件处理程序并在那里调整z-index 来实现相同的效果。

Here's a working example 这是一个工作示例

A bit more info is available here. 此处提供更多信息

This solution works automatically with arbitrarily deeply stacks modals.此解决方案可自动与任意深度堆叠的模态一起使用。

The script source code:脚本源代码:

$(document).ready(function() {

    $('.modal').on('hidden.bs.modal', function(event) {
        $(this).removeClass( 'fv-modal-stack' );
        $('body').data( 'fv_open_modals', $('body').data( 'fv_open_modals' ) - 1 );
    });

    $('.modal').on('shown.bs.modal', function (event) {
        // keep track of the number of open modals
        if ( typeof( $('body').data( 'fv_open_modals' ) ) == 'undefined' ) {
            $('body').data( 'fv_open_modals', 0 );
        }

        // if the z-index of this modal has been set, ignore.
        if ($(this).hasClass('fv-modal-stack')) {
            return;
        }

        $(this).addClass('fv-modal-stack');
        $('body').data('fv_open_modals', $('body').data('fv_open_modals' ) + 1 );
        $(this).css('z-index', 1040 + (10 * $('body').data('fv_open_modals' )));
        $('.modal-backdrop').not('.fv-modal-stack').css('z-index', 1039 + (10 * $('body').data('fv_open_modals')));
        $('.modal-backdrop').not('fv-modal-stack').addClass('fv-modal-stack'); 

    });        
});

Combining A1rPun's answer with the suggestion by StriplingWarrior, I came up with this:结合 A1rPun 的回答和 StriplingWarrior 的建议,我想出了这个:

$(document).on({
    'show.bs.modal': function () {
        var zIndex = 1040 + (10 * $('.modal:visible').length);
        $(this).css('z-index', zIndex);
        setTimeout(function() {
            $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
        }, 0);
    },
    'hidden.bs.modal': function() {
        if ($('.modal:visible').length > 0) {
            // restore the modal-open class to the body element, so that scrolling works
            // properly after de-stacking a modal.
            setTimeout(function() {
                $(document.body).addClass('modal-open');
            }, 0);
        }
    }
}, '.modal');

Works even for dynamic modals added after the fact, and removes the second-scrollbar issue.甚至适用于事后添加的动态模态,并消除了第二个滚动条问题。 The most notable thing that I found this useful for was integrating forms inside modals with validation feedback from Bootbox alerts, since those use dynamic modals and thus require you to bind the event to document rather than to .modal, since that only attaches it to existing modals.我发现这很有用的最值得注意的事情是将模态内的表单与来自 Bootbox 警报的验证反馈集成在一起,因为它们使用动态模态,因此需要您将事件绑定到文档而不是 .modal,因为这只会将它附加到现有的模态。

Fiddle here.在这里摆弄。

Something shorter version based off Yermo Lamers' suggestion, this seems to work alright.根据 Yermo Lamers 的建议缩短了一些版本,这似乎没问题。 Even with basic animations like fade in/out and even crazy batman newspaper rotate.即使是基本的动画,如淡入/淡出,甚至疯狂的蝙蝠侠报纸旋转。 http://jsfiddle.net/ketwaroo/mXy3E/ http://jsfiddle.net/ketwaroo/mXy3E/

$('.modal').on('show.bs.modal', function(event) {
    var idx = $('.modal:visible').length;
    $(this).css('z-index', 1040 + (10 * idx));
});
$('.modal').on('shown.bs.modal', function(event) {
    var idx = ($('.modal:visible').length) -1; // raise backdrop after animation.
    $('.modal-backdrop').not('.stacked').css('z-index', 1039 + (10 * idx));
    $('.modal-backdrop').not('.stacked').addClass('stacked');
});

I created a Bootstrap plugin that incorporates a lot of the ideas posted here.我创建了一个 Bootstrap 插件,其中包含了这里发布的许多想法。

Demo on Bootply: http://www.bootply.com/cObcYInvpq Bootply 上的演示: http : //www.bootply.com/cObcYInvpq

Github: https://github.com/jhaygt/bootstrap-multimodal Github: https : //github.com/jhaygt/bootstrap-multimodal

It also addresses the issue with successive modals causing the backdrop to become darker and darker.它还解决了连续模态导致背景变得越来越暗的问题。 This ensures that only one backdrop is visible at any given time:这确保在任何给定时间只有一个背景是可见的:

if(modalIndex > 0)
    $('.modal-backdrop').not(':first').addClass('hidden');

The z-index of the visible backdrop is updated on both the show.bs.modal and hidden.bs.modal events:可见背景的 z-index 在show.bs.modalhidden.bs.modal事件上更新:

$('.modal-backdrop:first').css('z-index', MultiModal.BASE_ZINDEX + (modalIndex * 20));

When solving Stacking modals scrolls the main page when one is closed i found that newer versions of Bootstrap (at least since version 3.0.3) do not require any additional code to stack modals.当解决堆叠模态在关闭时滚动主页时​​,我发现较新版本的 Bootstrap(至少从 3.0.3 版开始)不需要任何额外的代码来堆叠模态。

You can add more than one modal (of course having a different ID) to your page.您可以向页面添加多个模态(当然具有不同的 ID)。 The only issue found when opening more than one modal will be that closing one remove the modal-open class for the body selector.打开多个模态时发现的唯一问题是关闭一个会删除主体选择器的modal-open类。

You can use the following Javascript code to re-add the modal-open :您可以使用以下 Javascript 代码重新添加modal-open

$('.modal').on('hidden.bs.modal', function (e) {
    if($('.modal').hasClass('in')) {
    $('body').addClass('modal-open');
    }    
});

In the case that do not need the backdrop effect for the stacked modal you can set data-backdrop="false" .在不需要堆叠模式的背景效果的情况下,您可以设置data-backdrop="false"

Version 3.1.1.版本 3.1.1。 fixed Fix modal backdrop overlaying the modal's scrollbar , but the above solution seems also to work with earlier versions.固定修复覆盖模态滚动条的模态背景,但上述解决方案似乎也适用于早期版本。

If you're looking for Bootstrap 4 solution, there's an easy one using pure CSS:如果您正在寻找 Bootstrap 4 解决方案,那么使用纯 CSS 有一个简单的解决方案:

.modal.fade {
    background: rgba(0,0,0,0.5);
}

A simple solution for Bootstrap 4.5 Bootstrap 4.5 的简单解决方案

.modal.fade {
  background: rgba(0, 0, 0, 0.5);
}

.modal-backdrop.fade {
  opacity: 0;
}

Finally solved.终于解决了。 I tested it in many ways and works fine.我以多种方式对其进行了测试,并且运行良好。

Here is the solution for anyone that have the same problem: Change the Modal.prototype.show function (at bootstrap.js or modal.js)这是任何有相同问题的人的解决方案:更改Modal.prototype.show函数(在 bootstrap.js 或 modal.js)

FROM:发件人:

if (transition) {
   that.$element[0].offsetWidth // force reflow
}   

that.$element
   .addClass('in')
   .attr('aria-hidden', false)

that.enforceFocus()

TO:致:

if (transition) {
    that.$element[0].offsetWidth // force reflow
}

that.$backdrop
   .css("z-index", (1030 + (10 * $(".modal.fade.in").length)))

that.$element
   .css("z-index", (1040 + (10 * $(".modal.fade.in").length)))
   .addClass('in')
   .attr('aria-hidden', false)

that.enforceFocus()

It's the best way that i found: check how many modals are opened and change the z-index of the modal and the backdrop to a higher value.这是我发现的最好方法:检查打开了多少模态并将模态和背景的 z-index 更改为更高的值。

Try adding the following to your JS on bootply尝试在 bootply 上将以下内容添加到您的 JS 中

$('#myModal2').on('show.bs.modal', function () {  
$('#myModal').css('z-index', 1030); })

$('#myModal2').on('hidden.bs.modal', function () {  
$('#myModal').css('z-index', 1040); })

Explanation:说明:

After playing around with the attributes(using Chrome's dev tool), I have realized that any z-index value below 1031 will put things behind the backdrop.在玩弄属性后(使用 Chrome 的开发工具),我意识到任何低于1031 z-index值都会把东西放在背景后面。

So by using bootstrap's modal event handles I set the z-index to 1030 .因此,通过使用引导程序的模态事件句柄,我将z-index设置为1030 If #myModal2 is shown and set the z-index back to 1040 if #myModal2 is hidden.如果#myModal2显示和设置z-index返回到1040 ,如果#myModal2是隐藏的。

Demo演示

My solution for bootstrap 4, working with unlimited depth of modals and dynamic modal.我的 bootstrap 4 解决方案,使用无限深度的模态和动态模态。

$('.modal').on('show.bs.modal', function () {
    var $modal = $(this);
    var baseZIndex = 1050;
    var modalZIndex = baseZIndex + ($('.modal.show').length * 20);
    var backdropZIndex = modalZIndex - 10;
    $modal.css('z-index', modalZIndex).css('overflow', 'auto');
    $('.modal-backdrop.show:last').css('z-index', backdropZIndex);
});
$('.modal').on('shown.bs.modal', function () {
    var baseBackdropZIndex = 1040;
    $('.modal-backdrop.show').each(function (i) {
        $(this).css('z-index', baseBackdropZIndex + (i * 20));
    });
});
$('.modal').on('hide.bs.modal', function () {
    var $modal = $(this);
    $modal.css('z-index', '');
});

Everytime you run sys.showModal function increment z-index and set it to your new modal.每次运行 sys.showModal 函数都会增加 z-index 并将其设置为新的模态。

function system() {

    this.modalIndex = 2000;

    this.showModal = function (selector) {
        this.modalIndex++;

        $(selector).modal({
            backdrop: 'static',
            keyboard: true
        });
        $(selector).modal('show');
        $(selector).css('z-index', this.modalIndex );       
    }

}

var sys = new system();

sys.showModal('#myModal1');
sys.showModal('#myModal2');

对我来说,解决这个问题的方法是不要在我的模态 div 上使用“淡入淡出”类。

No script solutions , using only css given you have two layers of modals, set the 2nd modal to a higher z index没有脚本解决方案,如果您有两层模态,则仅使用 css,将第二个模态设置为更高的 z 索引

.second-modal { z-index: 1070 }

div.modal-backdrop + div.modal-backdrop {
   z-index: 1060; 
}

If you want a specific modal to appear on top of another open modal, try adding the HTML of the topmost modal after the other modal div .如果您希望特定的模态出现在另一个打开的模态之上,请尝试在另一个模态div之后添加最顶层的模态的 HTML。

This worked for me:这对我有用:

<div id="modal-under" class="modal fade" ... />

<!--
This modal-upper should appear on top of #modal-under when both are open.
Place its HTML after #modal-under. -->
<div id="modal-upper" class="modal fade" ... />

Solution for Bootstrap 5 (pure JS). Bootstrap 5(纯 JS)的解决方案。

Solution inspired by the answers of @A1rPun.解决方案灵感来自@A1rPun 的答案。

// On modal open
document.addEventListener('show.bs.modal', function(e) {

    // Get count of opened modals
    let modalsCount = 1;
    document.querySelectorAll('.modal').forEach(function(modalElement) {
        if (modalElement.style.display == 'block') {
            modalsCount++;
        }
    });

    // Set modal and backdrop z-indexes
    const zIndex = 1055 + 10 * modalsCount;
    e.target.style.zIndex = zIndex;
    setTimeout(() => {
        const backdropNotStacked = document.querySelector('.modal-backdrop:not(.modal-stack)');
        backdropNotStacked.style.zIndex = ('z-index', zIndex - 5);
        backdropNotStacked.classList.add('modal-stack');
    });

});

Explanation解释

  1. loop all visible modals (you cannot use the pseudoselector :visible , which is only in jquery)循环所有可见的模式(你不能使用伪选择器:visible ,它只在 jquery 中)
  2. calculate new z-index.计算新的 z-index。 Default for Bootstrap 5 is 1055, so: Bootstrap 5 的默认值为 1055,因此:

default(1055) + 10 * number of opened modals default(1055) + 10 * 打开的模式数

  1. set this new calculated z-index to the modal将这个新计算的 z-index 设置为模态
  2. identify backdrop (backdrop without specified class - in our case .modal-stack )识别背景(没有指定背景 class - 在我们的例子中是 .modal-stack
  3. set this new calculated z-index -5 to the backdrop将这个新计算的 z-index -5 设置为背景
  4. add class .modal-stack to the backdrop to prevent getting this backdrop while opening next modal将 class .modal-stack添加到背景中,以防止在打开下一个模式时获得此背景

Each modal should be given a different id and each link should be targeted to a different modal id.每个模态应该被赋予不同的 id,并且每个链接都应该针对不同的模态 id。 So it should be something like that:所以它应该是这样的:

<a href="#myModal" data-toggle="modal">
...
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
...
<a href="#myModal2" data-toggle="modal">
...
<div id="myModal2" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
...

EDIT: Bootstrap 3.3.4 has solved this problem (and other modal issues) so if you can update your bootstrap CSS and JS that would be the best solution.编辑:Bootstrap 3.3.4 已经解决了这个问题(和其他模态问题),所以如果你能更新你的 bootstrap CSS 和 JS,那将是最好的解决方案。 If you can't update the solution below will still work and essentially does the same thing as bootstrap 3.3.4 (recalculate and apply padding).如果您无法更新,下面的解决方案仍然有效,并且基本上与 bootstrap 3.3.4 执行相同的操作(重新计算并应用填充)。

As Bass Jobsen pointed out, newer versions of Bootstrap have the z-index solved.正如 Bass Jobsen 指出的那样,较新版本的 Bootstrap 解决了 z-index 问题。 The modal-open class and padding-right were still problems for me but this scripts inspired by Yermo Lamers solution solves it. modal-open 类和 padding-right 对我来说仍然是问题,但是这个受 Yermo Lamers 解决方案启发的脚本解决了它。 Just drop it in your JS file and enjoy.只需将其放入您的 JS 文件中即可享受。

$(document).on('hide.bs.modal', '.modal', function (event) {
    var padding_right = 0;
    $.each($('.modal'), function(){
        if($(this).hasClass('in') && $(this).modal().data('bs.modal').scrollbarWidth > padding_right) {
            padding_right = $(this).modal().data('bs.modal').scrollbarWidth
        }
    });
    $('body').data('padding_right', padding_right + 'px');
});

$(document).on('hidden.bs.modal', '.modal', function (event) {
    $('body').data('open_modals', $('body').data('open_modals') - 1);
    if($('body').data('open_modals') > 0) {
        $('body').addClass('modal-open');
        $('body').css('padding-right', $('body').data('padding_right'));
    }
});

$(document).on('shown.bs.modal', '.modal', function (event) {
    if (typeof($('body').data('open_modals')) == 'undefined') {
        $('body').data('open_modals', 0);
    }
    $('body').data('open_modals', $('body').data('open_modals') + 1);
    $('body').css('padding-right', (parseInt($('body').css('padding-right')) / $('body').data('open_modals') + 'px'));
});

work for open/close multi modals为打开/关闭多模式工作

jQuery(function()
{
    jQuery(document).on('show.bs.modal', '.modal', function()
    {
        var maxZ = parseInt(jQuery('.modal-backdrop').css('z-index')) || 1040;

        jQuery('.modal:visible').each(function()
        {
            maxZ = Math.max(parseInt(jQuery(this).css('z-index')), maxZ);
        });

        jQuery('.modal-backdrop').css('z-index', maxZ);
        jQuery(this).css("z-index", maxZ + 1);
        jQuery('.modal-dialog', this).css("z-index", maxZ + 2);
    });

    jQuery(document).on('hidden.bs.modal', '.modal', function () 
    {
        if (jQuery('.modal:visible').length)
        {
            jQuery(document.body).addClass('modal-open');

           var maxZ = 1040;

           jQuery('.modal:visible').each(function()
           {
               maxZ = Math.max(parseInt(jQuery(this).css('z-index')), maxZ);
           });

           jQuery('.modal-backdrop').css('z-index', maxZ-1);
       }
    });
});

Demo演示

https://www.bootply.com/cObcYInvpq# https://www.bootply.com/cObcYInvpq#

Check this out!看看这个! This solution solved the problem for me, few simple CSS lines:这个解决方案为我解决了这个问题,几行简单的 CSS 代码:

.modal:nth-of-type(even) {
z-index: 1042 !important;
}
.modal-backdrop.in:nth-of-type(even) {
    z-index: 1041 !important;
}

Here is a link to where I found it: Bootply Just make sure that the .modual that need to appear on Top is second in HTML code, so CSS can find it as "even".这是我找到它的链接: Bootply只要确保需要出现在顶部的 .modual 在 HTML 代码中排在第二位,这样 CSS 就可以找到它作为“偶数”。

For me, these simple scss rules worked perfectly:对我来说,这些简单的 scss 规则非常有效:

.modal.show{
  z-index: 1041;
  ~ .modal.show{
    z-index: 1043;
  }
}
.modal-backdrop.show {
  z-index: 1040;
  + .modal-backdrop.show{
    z-index: 1042;
  }
}

If these rules cause the wrong modal to be on top in your case, either change the order of your modal divs, or change (odd) to (even) in above scss.如果这些规则导致错误的模态出现在您的情况下,请更改模态 div 的顺序,或者在上面的 scss 中将(奇数)更改为(偶数)。

Note: all answers are "hacks" since Bootstrap doesn't officially support multiple modals..注意:所有答案都是“黑客”,因为 Bootstrap 不正式支持多种模式。

"Bootstrap only supports one modal window at a time. Nested modals aren't supported as we believe them to be poor user experiences." “Bootstrap 一次仅支持一个模态窗口。不支持嵌套模态,因为我们认为它们的用户体验很差。”

Here are some CSS workarounds/hacks...这里有一些CSS解决方法/技巧...

Bootstrap 5 beta (Update 2021) Bootstrap 5 测试版(2021 年更新)

The default z-index for modals has changed again to 1060. Therefore, to override the modals and backdrop use..模态的默认 z-index 再次更改为 1060。因此,要覆盖模态和背景,请使用..

.modal:nth-of-type(even) {
    z-index: 1062 !important;
}
.modal-backdrop.show:nth-of-type(even) {
    z-index: 1061 !important;
}

https://codeply.com/p/yNgonlFihM https://codeply.com/p/yNgonlFihM


The z-index for modals in Bootstrap 4 has changed again to 1050. Therefore, to override the open modals and backdrop use. Bootstrap 4 中模态的 z-index 再次更改为 1050。因此,要覆盖打开的模态和背景使用。

Bootstrap 4.x (Update 2018) Bootstrap 4.x(2018 年更新)

.modal:nth-of-type(even) {
    z-index: 1052 !important;
}
.modal-backdrop.show:nth-of-type(even) {
    z-index: 1051 !important;
}

https://codeply.com/p/29sH0ofTZb https://codeply.com/p/29sH0ofTZb


Bootstrap 3.x (Original Answer) Bootstrap 3.x(原始答案)

Here is some CSS using nth-of-type selectors that seems to work:这是一些使用nth-of-type选择器的 CSS 似乎有效:

    .modal:nth-of-type(even) {
        z-index: 1042 !important;
    }
    .modal-backdrop.in:nth-of-type(even) {
        z-index: 1041 !important;
    }

https://codeply.com/p/w8yjOM4DFb https://codeply.com/p/w8yjOM4DFb

I had a similar scenario, and after a little bit of R&D I found a solution.我有一个类似的场景,经过一些 R&D 我找到了一个解决方案。 Although I'm not great in JS still I have managed to write down a small query.虽然我在 JS 方面不是很好,但我还是设法写下了一个小查询。

http://jsfiddle.net/Sherbrow/ThLYb/ http://jsfiddle.net/Sherbrow/ThLYb/

<div class="ingredient-item" data-toggle="modal" data-target="#myModal">test1 <p>trerefefef</p></div>
<div class="ingredient-item" data-toggle="modal" data-target="#myModal">tst2 <p>Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum</p></div>
<div class="ingredient-item" data-toggle="modal" data-target="#myModal">test3 <p>afsasfafafsa</p></div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</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>
</div>





$('.ingredient-item').on('click', function(e){

   e.preventDefault();

    var content = $(this).find('p').text();

    $('.modal-body').html(content);

});

Add global variable in modal.js在 modal.js 中添加全局变量

var modalBGIndex = 1040; // modal backdrop background
var modalConIndex = 1042; // modal container data 

// show function inside add variable - Modal.prototype.backdrop // 在添加变量中显示函数 - Modal.prototype.backdrop

var e    = $.Event('show.bs.modal', { relatedTarget: _relatedTarget })

modalConIndex = modalConIndex + 2; // add this line inside "Modal.prototype.show"

that.$element
    .show()
    .scrollTop(0)
that.$element.css('z-index',modalConIndex) // add this line after show modal 

if (this.isShown && this.options.backdrop) {
      var doAnimate = $.support.transition && animate

      modalBGIndex = modalBGIndex + 2; // add this line increase modal background index 2+

this.$backdrop.addClass('in')
this.$backdrop.css('z-index',modalBGIndex) // add this line after backdrop addclass

The other solutions did not work for me out of the box.其他解决方案对我来说开箱即用。 I think perhaps because I am using a more recent version of Bootstrap (3.3.2).... the overlay was appearing on top of the modal dialog.我想也许是因为我使用的是更新版本的 Bootstrap (3.3.2)....覆盖出现模态对话框的顶部

I refactored the code a bit and commented out the part that was adjusting the modal-backdrop.我稍微重构了代码并注释掉了调整模态背景的部分。 This fixed the issue.这解决了这个问题。

    var $body = $('body');
    var OPEN_MODALS_COUNT = 'fv_open_modals';
    var Z_ADJUSTED = 'fv-modal-stack';
    var defaultBootstrapModalZindex = 1040;

    // keep track of the number of open modals                   
    if ($body.data(OPEN_MODALS_COUNT) === undefined) {
        $body.data(OPEN_MODALS_COUNT, 0);
    }

    $body.on('show.bs.modal', '.modal', function (event)
    {
        if (!$(this).hasClass(Z_ADJUSTED))  // only if z-index not already set
        {
            // Increment count & mark as being adjusted
            $body.data(OPEN_MODALS_COUNT, $body.data(OPEN_MODALS_COUNT) + 1);
            $(this).addClass(Z_ADJUSTED);

            // Set Z-Index
            $(this).css('z-index', defaultBootstrapModalZindex + (1 * $body.data(OPEN_MODALS_COUNT)));

            //// BackDrop z-index   (Doesn't seem to be necessary with Bootstrap 3.3.2 ...)
            //$('.modal-backdrop').not( '.' + Z_ADJUSTED )
            //        .css('z-index', 1039 + (10 * $body.data(OPEN_MODALS_COUNT)))
            //        .addClass(Z_ADJUSTED);
        }
    });
    $body.on('hidden.bs.modal', '.modal', function (event)
    {
        // Decrement count & remove adjusted class
        $body.data(OPEN_MODALS_COUNT, $body.data(OPEN_MODALS_COUNT) - 1);
        $(this).removeClass(Z_ADJUSTED);
        // Fix issue with scrollbar being shown when any modal is hidden
        if($body.data(OPEN_MODALS_COUNT) > 0)
            $body.addClass('modal-open');
    });

As a side note, if you want to use this in AngularJs, just put the code inside of your module's .run() method.附带说明一下,如果您想在 AngularJs 中使用它,只需将代码放在模块的 .run() 方法中。

In my case the problem was caused by a browser extension that includes the bootstrap.js files where the show event handled twice and two modal-backdrop divs are added, but when closing the modal only one of them is removed.在我的情况下,问题是由包含 bootstrap.js 文件的浏览器扩展引起的,其中显示事件处理了两次并添加了两个modal-backdrop div,但在关闭模态时仅删除其中一个。

Found that by adding a subtree modification breakpoint to the body element in chrome, and tracked adding the modal-backdrop divs.发现通过在chrome中的body元素添加子树修改断点,并跟踪添加modal-backdrop div。

$(window).scroll(function(){
    if($('.modal.in').length && !$('body').hasClass('modal-open'))
    {
              $('body').addClass('modal-open');
    }

});

Update: 22.01.2019, 13.41 I optimized the solution by jhay, which also supports closing and opening same or different dialogs when for example stepping from one detail data to another forwards or backwards.更新:22.01.2019, 13.41 我优化了 jhay 的解决方案,它还支持关闭和打开相同或不同的对话框,例如从一个细节数据向前或向后步进时。

(function ($, window) {
'use strict';

var MultiModal = function (element) {
    this.$element = $(element);
    this.modalIndex = 0;
};

MultiModal.BASE_ZINDEX = 1040;

/* Max index number. When reached just collate the zIndexes */
MultiModal.MAX_INDEX = 5;

MultiModal.prototype.show = function (target) {
    var that = this;
    var $target = $(target);

    // Bootstrap triggers the show event at the beginning of the show function and before
    // the modal backdrop element has been created. The timeout here allows the modal
    // show function to complete, after which the modal backdrop will have been created
    // and appended to the DOM.

    // we only want one backdrop; hide any extras
    setTimeout(function () {
        /* Count the number of triggered modal dialogs */
        that.modalIndex++;

        if (that.modalIndex >= MultiModal.MAX_INDEX) {
            /* Collate the zIndexes of every open modal dialog according to its order */
            that.collateZIndex();
        }

        /* Modify the zIndex */
        $target.css('z-index', MultiModal.BASE_ZINDEX + (that.modalIndex * 20) + 10);

        /* we only want one backdrop; hide any extras */
        if (that.modalIndex > 1) 
            $('.modal-backdrop').not(':first').addClass('hidden');

        that.adjustBackdrop();
    });

};

MultiModal.prototype.hidden = function (target) {
    this.modalIndex--;
    this.adjustBackdrop();

    if ($('.modal.in').length === 1) {

        /* Reset the index to 1 when only one modal dialog is open */
        this.modalIndex = 1;
        $('.modal.in').css('z-index', MultiModal.BASE_ZINDEX + 10);
        var $modalBackdrop = $('.modal-backdrop:first');
        $modalBackdrop.removeClass('hidden');
        $modalBackdrop.css('z-index', MultiModal.BASE_ZINDEX);

    }
};

MultiModal.prototype.adjustBackdrop = function () {        
    $('.modal-backdrop:first').css('z-index', MultiModal.BASE_ZINDEX + (this.modalIndex * 20));
};

MultiModal.prototype.collateZIndex = function () {

    var index = 1;
    var $modals = $('.modal.in').toArray();


    $modals.sort(function(x, y) 
    {
        return (Number(x.style.zIndex) - Number(y.style.zIndex));
    });     

    for (i = 0; i < $modals.length; i++)
    {
        $($modals[i]).css('z-index', MultiModal.BASE_ZINDEX + (index * 20) + 10);
        index++;
    };

    this.modalIndex = index;
    this.adjustBackdrop();

};

function Plugin(method, target) {
    return this.each(function () {
        var $this = $(this);
        var data = $this.data('multi-modal-plugin');

        if (!data)
            $this.data('multi-modal-plugin', (data = new MultiModal(this)));

        if (method)
            data[method](target);
    });
}

$.fn.multiModal = Plugin;
$.fn.multiModal.Constructor = MultiModal;

$(document).on('show.bs.modal', function (e) {
    $(document).multiModal('show', e.target);
});

$(document).on('hidden.bs.modal', function (e) {
    $(document).multiModal('hidden', e.target);
});}(jQuery, window));

Check count of modals and add the value to backdrop as z-index检查模态数并将值添加到背景中作为 z-index

    var zIndex = 1500 + ($('.modal').length*2) + 1;
    this.popsr.css({'z-index': zIndex});

    this.popsr.on('shown.bs.modal', function () {
        $(this).next('.modal-backdrop').css('z-index', zIndex - 1);
    });

    this.popsr.modal('show');

This code just works perfectly for bootstrap 4. The problem in other codes were how the modal-backdrop is selected.此代码仅适用于引导程序 4。其他代码中的问题是如何选择模态背景。 It'll be better if you used the jQuery next select on the actual modal after the modal has been shown.如果您在显示模态后在实际模态上使用 jQuery next select 会更好。

 $(document).on('show.bs.modal', '.modal', function () { var zIndex = 1040 + (10 * $('.modal').length); var model = $(this); model.css('z-index', zIndex); model.attr('data-z-index', zIndex); }); $(document).on('shown.bs.modal', '.modal', function () { var model = $(this); var zIndex = model.attr('data-z-index'); model.next('.modal-backdrop.show').css('z-index', zIndex - 1); });

A1rPun's answer works perfectly after a minor modification (Bootstrap 4.6.0). A1rPun 的答案在稍作修改后完美运行(Bootstrap 4.6.0)。 My reputation won't let me comment, so I'll post an answer.我的声誉不允许我发表评论,所以我会发布一个答案。

I just replaced every .modal:visible for .modal.show .我刚刚更换一次.modal:visible.modal.show

So, to fix the backdrop when opening multiple modals:因此,要在打开多个模态时修复背景:

$(document).on('show.bs.modal', '.modal', function () {
    var zIndex = 1040 + (10 * $('.modal.show').length);
    $(this).css('z-index', zIndex);
    setTimeout(function() {
        $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
    }, 0);
});

And, to fix the scrollbar:并且,要修复滚动条:

$(document).on('hidden.bs.modal', '.modal', function () {
    $('.modal.show').length && $(document.body).addClass('modal-open');
});

Based on the example fiddle of this answer , I updated it to support bootstrap 3 and 4 and fix all issues mentioned at the comments there.基于这个答案的示例小提琴,我更新了它以支持引导程序 3 和 4 并修复那里的评论中提到的所有问题。 As i noticed them also, because i have some modals that have a timeout and close automatically.正如我也注意到它们的那样,因为我有一些具有超时并自动关闭的模态。

It will not work with bootstrap 5. Bootstrap 5 doesn't store the bs.modal object anymore using node.data('bs.modal') .它不适用于 bootstrap bs.modal 5 不再使用node.data('bs.modal')存储bs.modal对象。

I suggest, viewing the snippet in full screen.我建议,全屏查看代码片段。

Bootstrap 3 using the same example as the answer mentiond, except that dialog 4 is modified. Bootstrap 3 使用与上述答案相同的示例,只是修改了对话框 4。

 !function () { var z = "bs.modal.z-index.base", re_sort = function (el) { Array.prototype.slice.call($('.modal.show,.modal.in').not(el)) .sort(function (a, b) { // sort by z-index lowest to highest return +a.style.zIndex - +b.style.zIndex }) .forEach(function (el, idx) { // re-set the z-index based on the idx el.style.zIndex = $(el).data(z) + (2 * idx); const b = $(el).data('bs.modal')._backdrop || $(el).data("bs.modal").$backdrop; if (b) { $(b).css("z-index", +el.style.zIndex - 1); } }); }; $(document).on('show.bs.modal', '.modal', function (e) { // removing the currently set zIndex if any this.style.zIndex = ''; /* * should be 1050 always, if getComputedStyle is not supported use 1032 as variable... * * see https://getbootstrap.com/docs/4.0/layout/overview/#z-index and adjust the * other values to higher ones, if required * * Bootstrap 3: https:////netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css .modal { [...] z-index: 1050; [...] } .modal-backdrop { [...] z-index: 1040; [...] } * Bootstrap 4: https://getbootstrap.com/docs/4.0/layout/overview/#z-index * * * lowest value which doesn't interfer with other bootstrap elements * since we manipulate the z-index of the backdrops too we need two for each modal * using 1032 you could open up to 13 modals without overlapping popovers */ if (!$(this).data(z)) { let def = +getComputedStyle(this).zIndex; // 1050 by default def = 1032; $(this).data(z, def); } // resort all others, except this re_sort(this); // 2 is fine 1 layer for the modal, 1 layer for the backdrop var zIndex = $(this).data(z) + (2 * $('.modal.show,.modal.in').not(this).length); e.target.style.zIndex = zIndex; /* * Bootstrap itself stores the var using jQuery data property the backdrop * is present there, even if it may not be attached to the DOM * * If it is not present, wait for it, using requestAnimationFrame loop */ const waitForBackdrop = function () { try { // can fail to get the config if the modal is opened for the first time const config = $(this).data('bs.modal')._config || $(this).data('bs.modal').options; if (config.backdrop != false) { const node = $(this).data('bs.modal')._backdrop || $(this).data("bs.modal").$backdrop; if (node) { $(node).css('z-index', +this.style.zIndex - 1); } else { window.requestAnimationFrame(waitForBackdrop); } } } catch (e) { window.requestAnimationFrame(waitForBackdrop); } }.bind(this); waitForBackdrop(); }); $(document).on("shown.bs.modal", ".modal", function () { re_sort(); }); $(document).on('hidden.bs.modal', '.modal', function (event) { this.style.zIndex = ''; // when hidden, remove the z-index if (this.isConnected) { const b = $(this).data('bs.modal')._backdrop || $(this).data("bs.modal").$backdrop; if (b) { $(b).css("z-index", ''); } } re_sort(); // if still backdrops are present at dom - readd modal-open if ($('.modal-backdrop.show,.modal-backdrop.in').length) $(document.body).addClass("modal-open"); }) }();
 /* crazy batman newspaper spinny thing */ .rotate { transform:rotate(180deg); transition:all 0.25s; } .rotate.in { transform:rotate(1800deg); transition:all 0.75s; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script> <link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"/> <h2>Stacked Bootstrap Modal Example.</h2> <a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a> <div class="modal fade" id="myModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Modal 1</h4> </div> <div class="container"></div> <div class="modal-body">Content for the dialog / modal goes here. <br> <br> <br> <p>more content</p> <br> <br> <br> <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <div class="modal fade rotate" id="myModal2"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Modal 2</h4> </div> <div class="container"></div> <div class="modal-body">Content for the dialog / modal goes here. <br> <br> <p>come content</p> <br> <br> <br> <a data-toggle="modal" href="#myModal3" class="btn btn-primary">Launch modal</a> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <div class="modal fade" id="myModal3"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Modal 3</h4> </div> <div class="container"></div> <div class="modal-body">Content for the dialog / modal goes here. <br> <br> <br> <br> <br> <a data-toggle="modal" href="#myModal4" class="btn btn-primary">Launch modal</a> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <div class="modal fade" id="myModal4"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Modal 4</h4> </div> <div class="container"></div> <div class="modal-body"> <button onclick="$('#myModal').modal('hide');" class="btn btn-primary">hide #1</button> <button onclick="$('#myModal').modal('show');" class="btn btn-primary">show #1</button> <br> <button onclick="$('#myModal2').modal('hide');" class="btn btn-primary">hide #2</button> <button onclick="$('#myModal2').modal('show');" class="btn btn-primary">show #2</button> <br> <button onclick="$('#myModal3').modal('hide');" class="btn btn-primary">hide #3</button> <button onclick="$('#myModal3').modal('show');" class="btn btn-primary">show #3</button> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div>

Bootstrap 4 (see Bootstrap 3 snippet for commented code) Bootstrap 4(有关注释代码,请参阅 Bootstrap 3 代码段)

 !function () { var z = "bs.modal.z-index.base", re_sort = function (el) { Array.prototype.slice.call($('.modal.show,.modal.in').not(el)) .sort(function (a, b) { return +a.style.zIndex - +b.style.zIndex }) .forEach(function (el, idx) { el.style.zIndex = $(el).data(z) + (2 * idx); const b = $(el).data('bs.modal')._backdrop || $(el).data("bs.modal").$backdrop; if (b) { $(b).css("z-index", +el.style.zIndex - 1); } }); }; $(document).on('show.bs.modal', '.modal', function (e) { this.style.zIndex = ''; if (!$(this).data(z)) { let def = +getComputedStyle(this).zIndex; def = 1032; $(this).data(z, def); } re_sort(this); var zIndex = $(this).data(z) + (2 * $('.modal.show,.modal.in').not(this).length); e.target.style.zIndex = zIndex; const waitForBackdrop = function () { try { const config = $(this).data('bs.modal')._config || $(this).data('bs.modal').options; if (config.backdrop != false) { const node = $(this).data('bs.modal')._backdrop || $(this).data("bs.modal").$backdrop; if (node) { $(node).css('z-index', +this.style.zIndex - 1); } else { window.requestAnimationFrame(waitForBackdrop); } } } catch (e) { window.requestAnimationFrame(waitForBackdrop); } }.bind(this); waitForBackdrop(); }); $(document).on("shown.bs.modal", ".modal", function () { re_sort(); }); $(document).on('hidden.bs.modal', '.modal', function (event) { this.style.zIndex = ''; if (this.isConnected) { const b = $(this).data('bs.modal')._backdrop || $(this).data("bs.modal").$backdrop; if (b) { $(b).css("z-index", ''); } } re_sort(); if ($('.modal-backdrop.show,.modal-backdrop.in').length) $(document.body).addClass("modal-open"); }) }(); // creates dynamic modals i used this for stuff like // `enterSomething('stuff','to','display').then(...)` !function() { let a = (i, a) => Array.prototype.forEach.call(a, (e) => $('#' + i + '-modal').find('.modal-body').append(e)), b = function () { $(this).remove() }, c = (i, a) => Array.prototype.forEach.call(a, (e) => $('#' + i + '-modal-text-container').append(e)), r = () => 'dialog-' + (Date.now() + '-' + Math.random()).replace('.', '-'); this.createModal = function createModal() { let id = r(); $(document.body).append('<div class="modal fade" tabindex="-1" role="dialog" data-backdrop="static" aria-hidden="true" id="' + id + '-modal"><div class="modal-dialog d-flex modal-xl"><div class="modal-content align-self-stretch" style="overflow: hidden; max-height: -webkit-fill-available;"><div class="modal-header py-1"><h5 class="modal-header-text p-0 m-0"></h5><button id="' + id + '-modal-btn-close" type="button" tabindex="-1" class="close" data-dismiss="modal" aria-label="Close" title="Close"><span aria-hidden="true">&times;</span></button></div><div class="modal-body py-2"></div><div class="modal-footer py-1"><button type="button" class="btn btn-primary btn-sm" id="' + id + '-modal-btn-ok">Okay</button></div></div></div></div>'); $('#' + id + '-modal-btn-ok').on('click', () => $('#' + id + '-modal').modal('hide')); $('#' + id + '-modal').on('shown.bs.modal', () => $('#' + id + '-modal-btn-ok').focus()).on('hidden.bs.modal', b).modal('show'); $('#' + id + '-modal').find(".modal-header-text").html("Title"); a(id, arguments); return new Promise((r) => $('#' + id + '-modal').on('hide.bs.modal', () => r())); } }(); function another() { createModal( $("<button class='btn mx-1'>Another...</button>").on("click", another), $("<button class='btn mx-1'>Close lowest</button>").on("click", closeLowest), $("<button class='btn mx-1'>Bring lowest to front</button>").on("click", lowestToFront), $("<p>").text($(".modal.show,.modal.in").length) ).then(() => console.log("modal closed")); // only for this example: $(".modal").last().css('padding-top', ($(".modal.show,.modal.in").length * 20) +'px'); } function closeLowest() { $(Array.prototype.slice.call($('.modal.show,.modal.in')) .sort(function (a, b) { // sort by z-index lowest to highest return +a.style.zIndex - +b.style.zIndex })).first().modal('hide'); } function lowestToFront() { $(Array.prototype.slice.call($('.modal.show,.modal.in')) .sort(function (a, b) { // sort by z-index lowest to highest return +a.style.zIndex - +b.style.zIndex })).first().trigger('show.bs.modal'); } another();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <p>Use inspecter to check z-index values</p> <button class="btn btn-outline-primary" onclick="another()">Click!</button>

z-index and modal-backdrop corrections with css z-indexmodal-backdrop校正 css

.modal.fade {
  z-index: 10000000 !important;
  background: rgba(0, 0, 0, 0.5);
}
.modal-backdrop.fade {
  opacity: 0;
}

I need that the overlay shows above the first modal, not in the back.我需要将叠加层显示在第一个模态上方,而不要显示在背面。

模态叠加在后面

 $('#openBtn').click(function(){ $('#myModal').modal({show:true}) });
 <a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a> <div class="modal" id="myModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Modal title</h4> </div><div class="container"></div> <div class="modal-body"> Content for the dialog / modal goes here. <br> <br> <br> <br> <br> <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <div class="modal" id="myModal2" data-backdrop="static"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">Second Modal title</h4> </div><div class="container"></div> <div class="modal-body"> Content for the dialog / modal goes here. </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> <a href="#" class="btn btn-primary">Save changes</a> </div> </div> </div> </div> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/css/bootstrap.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0/js/bootstrap.min.js"></script>

I tried to change the z-index of .modal-backdrop , but it becomes a mess.我试图更改.modal-backdropz-index ,但是它变得一团糟。

In some cases I have more than two modals on the same page.在某些情况下,我在同一页面上有两个以上的模态。

please follow instruction 请遵循指示
1. first give any id to your modal popup which you want to give perioty eX: id='testmodal' 1.首先给你的模态弹出窗口提供任何id你要给perioty eX:id ='testmodal'
2. In style you can defile a css like below, value 2147483647 is the highest value which you can give as az index. 2.在风格上你可以玷污下面的css,值2147483647是你可以作为az索引给出的最高值。

#testmodal.fade.in{
    z-index: 2147483647;
}

this css class will apply on your modal in which you want to apply,because it will first search id=testmodal if you have more then one popup then you can set "z-index" as per your priority ,"mean higher priority will get higher z-index value 这个css类将适用于你想要应用的模态,因为如果你有一个以上的弹出窗口,它将首先搜索id = testmodal然后你可以根据你的优先级设置“z-index”,“意味着更高的优先级将获得更高的z指数值

这是一个非常古老的威胁,但是,对我来说,只是将我想要的模态的 html 代码移到文件中的最前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM