本文實例為大家分享了BootStrap模態框不垂直居中的解決方法,供大家參考,具體內容如下
解決問題:BootStrap自帶的模態框不垂直居中
解決方案:調用BootStrap為我們提供的方法$('.modal').on('show.bs.modal', function(){});
在模態框顯示之前我們用JS修改他的Top值,
具體代碼如下:
/** * 垂直居中模態框 **/ function centerModals() { $('.modal').each(function(i) { var $clone = $(this).clone().css('display', 'block').appendTo('body'); var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2); top = top > 50 ? top : 0; $clone.remove(); $(this).find('.modal-content').css("margin-top", top - 50); }); } // 在模態框出現的時候調用垂直居中方法 $('.modal').on('show.bs.modal', centerModals); // 在窗口大小改變的時候調用垂直居中方法 $(window).on('resize', centerModals);
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com