本文實例為大家分享了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); 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選