1 /**
2 * 使页面中所有.modal元素在窗口可视范围之内居中
3 **/
4 function centerModals(){
5 $(\'.modal\').each(function(i){
6 var $clone = $(this).clone().css(\'display\', \'block\').appendTo(\'body\');
7 var top = Math.round(($clone.height() - $clone.find(\'.modal-content\').height()) / 2);
8 top = top > 50 ? top : 0;
9 $clone.remove();
10 $(this).find(\'.modal-content\').css("margin-top", top-50);
11 });
12 }
13 // 在模态框出现的时候调用垂直居中函数
14 $(\'.modal\').on(\'show.bs.modal\', centerModals);
15 // 在窗口大小改变的时候调用垂直居中函数
16 $(window).on(\'resize\', centerModals);