您的位置:首页 > Web前端 > BootStrap

bootstrap 模态框垂直居中

2016-12-06 11:04 357 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: