bootstrap實現巢狀模態框的例項程式碼
阿新 • • 發佈:2020-01-12
專案上有一個需求,需要在已經彈出的一個bootstrap模態框的基礎上再彈一個模態框。
因為bootstrap官方不建議這麼做,最後實現的過程屬實不易。
以下是解決方案:
html程式碼:
<div id="container"> <a data-toggle="modal" href="#myModal" rel="external nofollow" class="btn btn-primary">彈出第一層模態框</a> <!-- 第一層模態框 --> <div class="modal fade" id="myModal"> <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">第一層模態框</h4> </div> <div class="container"></div> <div class="modal-body"> <p>第一層模態框</p> <br> <a data-toggle="modal" href="#myModal2" rel="external nofollow" class="btn btn-primary">彈出第二層模態框</a> </div> <div class="modal-footer"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-dismiss="modal" class="btn">關閉</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-primary">儲存</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">第二層模態框</h4> </div> <div class="container"></div> <div class="modal-body"> <p>第二層模態框</p> </div> <div class="modal-footer"> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-dismiss="modal" class="btn">關閉</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-primary">儲存</a> </div> </div> </div> </div> <!-- 遮罩 --> <div id="cover"></div> </div>
遮罩的css樣式:
<style type="text/css"> <!-- 遮罩是為了第二層模態框彈出時,可以將第一層模態框遮住 --> #cover { display: none; position: fixed; background: #000000; left: 0; top: 0; width: 100%; height: 100%; opacity: 0.40; z-index: 1 } </style>
js程式碼:
$(document).ready(function (){ //第二層模態框彈出時,為其設定一個大於第一層模態框的z-index //使得第二層模態框可以在第一層模態框上面 $(document).on('show.bs.modal','#myModal2',function (event) { var zIndex = 1040 + (10 * $('.modal:visible').length+1); $(this).css('z-index',zIndex); //開啟遮罩,遮罩的高度小於第二層模態框 $("#cover").css('z-index',zIndex-1) $('#cover').css('display','block'); //顯示遮罩層 }); $('#myModal2').on('hide.bs.modal',function() { //第二層模態框關閉時,關閉遮罩 $('#cover').css('display','none'); }); });
總結
以上所述是小編給大家介紹的bootstrap實現巢狀模態框的例項程式碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!