多个模态叠加

多个模态叠加

我需要叠加显示在第一个模态上方,而不是在后面。

https://img2.mukewang.com/5d490e1b0001e47106990381.jpg

$('#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>

我试图改变z-index.modal-backdrop,但它成了一个烂摊子。

在某些情况下,我在同一页面上有两个以上的模态。


繁星点点滴滴
浏览 485回答 3
3回答

蝴蝶不菲

我意识到答案已经被接受,但我强烈建议不要黑客自举来解决这个问题。您可以通过挂钩shown.bs.modal和hidden.bs.modal事件处理程序并在那里调整z-index来轻松实现相同的效果。这是一个有效的例子这里有更多信息。此解决方案可自动使用任意深度堆栈模式。脚本源代码:$(document).ready(function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$('.modal').on('hidden.bs.modal',&nbsp;function(event)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(this).removeClass(&nbsp;'fv-modal-stack'&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$('body').data(&nbsp;'fv_open_modals',&nbsp;$('body').data(&nbsp;'fv_open_modals'&nbsp;)&nbsp;-&nbsp;1&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;$('.modal').on('shown.bs.modal',&nbsp;function&nbsp;(event)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;keep&nbsp;track&nbsp;of&nbsp;the&nbsp;number&nbsp;of&nbsp;open&nbsp;modals &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(&nbsp;typeof(&nbsp;$('body').data(&nbsp;'fv_open_modals'&nbsp;)&nbsp;)&nbsp;==&nbsp;'undefined'&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$('body').data(&nbsp;'fv_open_modals',&nbsp;0&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;if&nbsp;the&nbsp;z-index&nbsp;of&nbsp;this&nbsp;modal&nbsp;has&nbsp;been&nbsp;set,&nbsp;ignore. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($(this).hasClass('fv-modal-stack'))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(this).addClass('fv-modal-stack'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$('body').data('fv_open_modals',&nbsp;$('body').data('fv_open_modals'&nbsp;)&nbsp;+&nbsp;1&nbsp;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(this).css('z-index',&nbsp;1040&nbsp;+&nbsp;(10&nbsp;*&nbsp;$('body').data('fv_open_modals'&nbsp;))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$('.modal-backdrop').not('.fv-modal-stack').css('z-index',&nbsp;1039&nbsp;+&nbsp;(10&nbsp;*&nbsp;$('body').data('fv_open_modals'))); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$('.modal-backdrop').not('fv-modal-stack').addClass('fv-modal-stack');&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});
打开App,查看更多内容
随时随地看视频慕课网APP