在項目中,有幾個使用bootstrap模態框彈出問題,在單頁面上是居中顯示,但是嵌套在別的iframe中,彈出的位置在靠近頂部的位置。
查閱了一些資料,有幾種解決方案,1修改bootstrap的js文件,使彈出的位置居中 2在每個頁面彈出時,修改彈出的位置 3把bootstrap模態框彈出iframe的最外層
在實際操作中,使用第一種方式,會影響到其他使用到bootstrap的相關的未知影響,第三種方式,我雖然把數據彈到最外層,但是雙向綁定的數據不能傳遞,有些使用的插件效果也失效。所以我採用的是第二種方式。
可以通過監聽事件來控制單個模態框彈出位置
我們可以選擇show.bs.modal來控制模態框彈出位置,以下是實現代碼:
$('#myModalDialog').on('show.bs.modal', function(){
var $this = $(this);
var $modal_dialog = $this.find('.modal-dialog');
$this.css('display', 'block');
$modal_dialog.css({'margin-top': Math.max(0, ($(window).height() - $modal_dialog.height()) / 2) });
});
$("#myModalDialog").modal({backdrop: false, keyboard: false});