bootstrap modal 垂直居中对齐
文章参考
http://www.bubuko.com/infodetail-666582.html
http://v3.bootcss.com/JavaScript/#modals
- <div class="modal fade" id="sqh_model">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
- <h4 class="modal-title">蔬菜预约</h4>
- </div>
- <div class="modal-body">
- <p>尽请期待</p>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
- <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
- </div>
- </div><!-- /.modal-content -->
- </div><!-- /.modal-dialog -->
- </div><!-- /.modal -->
- function showTips(){
- $('#sqh_model').modal('show')
- }
默认是距离顶部30px,左右居中
如图所示

解决办法一:覆盖之前的CSS样式
- /**********对bootstrap的modal样式进行重写**********/
- .modal-dialog {
- margin: 200px auto;
- }
解决办法二:调用回调函数
- function showTips(){
- //{"backdrop":"static"}点击背景不会消失
- $('#sqh_model').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
- // 是弹出框居中。。。
- var $modal_dialog = $(this.$element[0]).find('.modal-dialog');
- //获取可视窗口的高度
- var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
- //得到dialog的高度
- var dialogHeight = $modal_dialog.height();
- //计算出距离顶部的高度
- var m_top = (clientHeight - dialogHeight)/2;
- console.log("clientHeight : " + clientHeight);
- console.log("dialogHeight : " + dialogHeight);
- console.log("m_top : " + m_top);
- $modal_dialog.css({'margin': m_top + 'px auto'});
- });
- }
解决办法三:修改源代码
- Modal.prototype.adjustDialog = function () {
- var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight
- this.$element.css({
- paddingLeft: !this.bodyIsOverflowing && modalIsOverflowing ? this.scrollbarWidth : '',
- paddingRight: this.bodyIsOverflowing && !modalIsOverflowing ? this.scrollbarWidth : ''
- });
- // 是弹出框居中。。。
- var $modal_dialog = $(this.$element[0]).find('.modal-dialog');
- //获取可视窗口的高度
- var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
- //得到dialog的高度
- var dialogHeight = $modal_dialog.height();
- //计算出距离顶部的高度
- var m_top = (clientHeight - dialogHeight)/2;
- console.log("clientHeight : " + clientHeight);
- console.log("dialogHeight : " + dialogHeight);
- console.log("m_top : " + m_top);
- $modal_dialog.css({'margin': m_top + 'px auto'});
- }
参数
可以将选项通过 data 属性或 javascript 代码传递。对于 data 属性,需要将参数名称放到 data- 之后,例如 data-backdrop=""。
| backdrop | boolean 或 字符串'static' |
true |
Includes a modal-backdrop element. Alternatively, specify the modal on click. |
| keyboard | boolean | true | 键盘上的 esc 键被按下时关闭模态框。 |
| show | boolean | true | 模态框初始化之后就立即显示出来。 |
| remote | path | false |
This option is deprecated since v3.3.0 and will be removed in v4. We recommend instead using client-side templating or a data binding framework, or calling jQuery.loadyourself. 如果提供的是 URL,将利用 jQuery 的 URL 地址加载要展示的内容(只加载一次)并 插入 还可以利用 复制
|
方法
.modal(options)
将页面中的某块内容作为模态框激活。接受可选参数 object。
$('#myModal').modal({
keyboard: false
})
.modal('toggle')
手动打开或关闭模态框。在模态框显示或隐藏之前返回到主调函数中(也就是,在触发 shown.bs.modal 或hidden.bs.modal 事件之前)。
$('#myModal').modal('toggle')
.modal('show')
手动打开模态框。在模态框显示之前返回到主调函数中 (也就是,在触发 shown.bs.modal 事件之前)。
$('#myModal').modal('show')
.modal('hide')
手动隐藏模态框。在模态框隐藏之前返回到主调函数中 (也就是,在触发 hidden.bs.modal 事件之前)。
$('#myModal').modal('hide')
.modal('handleUpdate')
Readjusts the modal's positioning to counter a scrollbar in case one should appear, which would make the modal jump to the left.
Only needed when the height of the modal changes while it is open.
$('#myModal').modal('handleUpdate')
事件
Bootstrap 的模态框类提供了一些事件用于监听并执行你自己的代码。
All modal events are fired at the modal itself (i.e. at the <div class="modal">).
| show.bs.modal |
的元素,则此元素可以通过事件的 |
| shown.bs.modal |
此事件在模态框已经显示出来(并且同时在 CSS 过渡效果完成)之后被触发。 如果是通过点击某个作为触发器的元素,则此元素可以通 过事件的 |
| hide.bs.modal | hide 方法调用之后立即触发该事件。 |
| hidden.bs.modal | 此事件在模态框被隐藏(并且同时在 CSS 过渡效果完成)之后被触发。 |
| loaded.bs.modal | 从远端的数据源加载完数据之后触发该事件。 |
$('#myModal').on('hidden.bs.modal', function (e) {
// do something...
})
bootstrap modal 垂直居中对齐的更多相关文章
- Bootstrap modal垂直居中
Bootstrap modal垂直居中 在网上看到有Bootstrap2的Modal dialog垂直居中问题解决方法,这种方法自己试了一下,并不能完全居中,并且窗口的大小不一样的话,每次显示的m ...
- bootstrap modal垂直居中(简单封装)
1.使用modal 弹出事件方法: 未封装前: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- Bootstrap Modal 垂直居中
Bootstrap 的 modal 正文中如果内容较少的话,并不会垂直居中,而是偏上, 如果想要达到垂直居中的效果,需要自动动手了. 可以在初始显示时设置垂直居中,可以这样做: $('#YourMod ...
- bootstrap modal垂直居中 (转)
根据博友的经验,总结后请使用方法一就行了 一,修改bootstrap.js 源码 原来的: Modal.prototype.adjustDialog = function () { ].scrollH ...
- Bootstrap模态框(modal)垂直居中
http://v3.bootcss.com/ 自己也试了改了几种方式也不容乐观,发现在窗口弹出之前是获取不到$(this).height()的值,本想着是用($(window).height()-$( ...
- Bootstrap(v3.2.0)模态框(modal)垂直居中
Bootstrap(v3.2.0)模态框(modal)垂直居中方法: 在bootstrap.js文件900行后面添加如下代码,便可以实现垂直居中. that.$element.children().e ...
- bootstrap Modal 模态框垂直居中
解决 Modal 垂直居中的问题,上网找了好多博客,有好多说改源码的,这个并没有实践. 但发现另一种解决办法,可以实现,代码如下: function centerModals(){ $('.modal ...
- Bootstrap3模态框Modal垂直居中样式
1,Bootstrap 模态框插件Bootbox垂直居中样式: <!DOCTYPE html> <html lang="en"> <head> ...
- html文本垂直居中对齐
html文本垂直居中对齐,代码如下: <div id="box" style="height:100px; line-height:100px; border:1p ...
随机推荐
- sencha touch 常见问题解答(1-25)
欢迎留言补充,持续更新中... 1.sencha touch 是什么? 答:Sencha touch框架是世界上第一个基于HTML 5的移动应用框架.它可以让你的Web应用看起来像网络应用.美丽的用户 ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十八:SDRAM模块① — 单字读写
实验十八:SDRAM模块① — 单字读写 笔者与SDRAM有段不短的孽缘,它作为冤魂日夜不断纠缠笔者.笔者尝试过许多方法将其退散,不过屡试屡败的笔者,最终心情像橘子一样橙.<整合篇>之际, ...
- javaAgent 参数
-javaagent 这个JVM参数是JDK 5引进的. Java -help的帮助里面写道: -javaagent:<jarpath>[=<options>] load Ja ...
- [SQL] SQL 日常检查脚本
--sqlserver 日常检查脚本 print '----------------------------' print ' 0.sqlserver all information ' print ...
- CentOS7.5搭建Solr7.4.0集群服务
一.Solr集群概念 solr单机版搭建参考: https://www.cnblogs.com/frankdeng/p/9615253.html 1.概念 SolrCloud(solr 云)是Solr ...
- centos 安装laravel
1.下载composer并全局安装 curl -sS https://getcomposer.org/installer | php 2.查看全局命令目录 echo $PATH 移动composer到 ...
- Mysql----索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Docker 学习应用篇之三: Docker的简单实用
安装完Docker之后,我们就可以简单的使用Docker,来体会Docker的用处. 首先看下Docker的常用命令,都是我在实用Docker的时候用到的命令: docker常用命令: $ docke ...
- Sublime2或3配置R、Scala、Python交互式环境
1.Sublime3的下载地址:http://www.sublimetext.com/3 2.刚刚安装的软件是没有PackageControl的,需要在新安装使用 (1) 以前没有安装过Packa ...
- ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds Me ...