Bootstrap modal 模态框垂直居中
来源:互联网 发布:淘宝助理二次验证 编辑:程序博客网 时间:2024/04/24 03:06
最近在使用Bootstrap框架,框架本身自带有一个modal模态框,也就是我们平时常叫的弹窗。但是这个模态框有一个特点,就是它本身是不会垂直居中的,默认只是距离顶部30px。那有没有办法让模态框垂直居中呢?有!
一个思路就是使用table布局,table布局的元素默认就是垂直居中的,在把外层.modal 元素设置为display: table ,里层.modal-dialog 元素设置为display: table-cell 之后却发现点击背后半透明的黑色遮罩无法使模态框消失了。所以table 方案 out!
另一个思路则是使用动态计算的方式,先贴上代码:
/*** 使页面中所有.modal元素在窗口可视范围之内居中**/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);
下面来进行解释,仔细观察会发现,centerModals 函数中在top判断时有一个50的高度,因为人的视觉在观察居中时,会有一点点偏差,这个数值可以根据个人的喜好去调整。
0 0
- Bootstrap模态框(modal)垂直居中
- Bootstrap模态框(modal)垂直居中
- Bootstrap modal 模态框垂直居中
- bootstrap模态框(modal)垂直居中
- bootstrap modal垂直居中
- bootstrap modal 垂直居中
- bootstrap modal垂直居中
- bootstrap modal 垂直居中对齐
- bootstrap modal 垂直居中对齐
- bootstrap modal 垂直居中对齐
- Bootstrap 模态框(modal)实现水平垂直居中显示
- Bootstrap弹出框(modal)垂直居中
- bootstrap modal弹出框的垂直居中
- bootstrap modal垂直居中(转)
- Bootstrap modal不能垂直居中的解决办法
- Bootstrap 模态框垂直居中
- bootstrap模态框 垂直居中
- bootstrap 模态框(modal)实现水平垂直居中显示 含具体分析
- rman全库备份,恢复过程
- HDOJ 1372 Knight Moves (BFS)
- 系统学习机器学习之非参数方法
- 《Android源码设计模式解析与实战》读书笔记(八)
- Android studio - experimental gradle 0.4.0.0 with openCV
- Bootstrap modal 模态框垂直居中
- 初到深圳工作的一些感受
- 系统学习机器学习之决策树
- HDOJ 1253 胜利大逃亡 (BFS)
- 汇编实现一位数加减乘除
- Jquery中使用setInterval和setTimeout
- Jog_P
- js 操作数组
- android ndk有用配置和调试