多个模态叠加我需要叠加显示在第一个模态上方,而不是在后面。$('#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,但它成了一个烂摊子。在某些情况下,我在同一页面上有两个以上的模态。
3 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
我意识到答案已经被接受,但我强烈建议不要黑客自举来解决这个问题。
您可以通过挂钩shown.bs.modal和hidden.bs.modal事件处理程序并在那里调整z-index来轻松实现相同的效果。
此解决方案可自动使用任意深度堆栈模式。
脚本源代码:
$(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'); }); });
添加回答
举报
0/150
提交
取消