文章参考

http://www.bubuko.com/infodetail-666582.html

http://v3.bootcss.com/JavaScript/#modals

  1. <div class="modal fade" id="sqh_model">
  2. <div class="modal-dialog">
  3. <div class="modal-content">
  4. <div class="modal-header">
  5. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  6. <h4 class="modal-title">蔬菜预约</h4>
  7. </div>
  8. <div class="modal-body">
  9. <p>尽请期待</p>
  10. </div>
  11. <div class="modal-footer">
  12. <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
  13. <button type="button" class="btn btn-primary" data-dismiss="modal">确定</button>
  14. </div>
  15. </div><!-- /.modal-content -->
  16. </div><!-- /.modal-dialog -->
  17. </div><!-- /.modal -->
  18. function showTips(){
  19. $('#sqh_model').modal('show')
  20. }

默认是距离顶部30px,左右居中

如图所示

解决办法一:覆盖之前的CSS样式

  1. /**********对bootstrap的modal样式进行重写**********/
  2. .modal-dialog {
  3. margin: 200px auto;
  4. }

解决办法二:调用回调函数

  1. function showTips(){
  2. //{"backdrop":"static"}点击背景不会消失
  3. $('#sqh_model').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
  4. // 是弹出框居中。。。
  5. var $modal_dialog = $(this.$element[0]).find('.modal-dialog');
  6. //获取可视窗口的高度
  7. var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
  8. //得到dialog的高度
  9. var dialogHeight = $modal_dialog.height();
  10. //计算出距离顶部的高度
  11. var m_top = (clientHeight - dialogHeight)/2;
  12. console.log("clientHeight : " + clientHeight);
  13. console.log("dialogHeight : " + dialogHeight);
  14. console.log("m_top : " + m_top);
  15. $modal_dialog.css({'margin': m_top + 'px auto'});
  16. });
  17. }

解决办法三:修改源代码

  1. Modal.prototype.adjustDialog = function () {
  2. var modalIsOverflowing = this.$element[0].scrollHeight > document.documentElement.clientHeight
  3. this.$element.css({
  4. paddingLeft:  !this.bodyIsOverflowing && modalIsOverflowing ? this.scrollbarWidth : '',
  5. paddingRight: this.bodyIsOverflowing && !modalIsOverflowing ? this.scrollbarWidth : ''
  6. });
  7. // 是弹出框居中。。。
  8. var $modal_dialog = $(this.$element[0]).find('.modal-dialog');
  9. //获取可视窗口的高度
  10. var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight: document.documentElement.clientHeight;
  11. //得到dialog的高度
  12. var dialogHeight = $modal_dialog.height();
  13. //计算出距离顶部的高度
  14. var m_top = (clientHeight - dialogHeight)/2;
  15. console.log("clientHeight : " + clientHeight);
  16. console.log("dialogHeight : " + dialogHeight);
  17. console.log("m_top : " + m_top);
  18. $modal_dialog.css({'margin': m_top + 'px auto'});
  19. }

参数

可以将选项通过 data 属性或 javascript 代码传递。对于 data 属性,需要将参数名称放到 data- 之后,例如 data-backdrop=""

名称 类型 默认值 描述

backdrop boolean 或 字符串'static' true

Includes a modal-backdrop element. Alternatively,

specify static for a backdrop which doesn't close

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 的 load 方法从此

URL 地址加载要展示的内容(只加载一次)并

插入 .modal-content 内。如果使用的是 data 属性 API,

还可以利用 href 属性指定内容来源地址。下面是一个实例:

复制
<a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a>

方法

.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

show 方法调用之后立即触发该事件。如果是通过点击某个作为触发器

的元素,则此元素可以通过事件的relatedTarget 属性进行访问。

shown.bs.modal

此事件在模态框已经显示出来(并且同时在 CSS 过渡效果完成)之后被触发。

如果是通过点击某个作为触发器的元素,则此元素可以通

过事件的 relatedTarget 属性进行访问。

hide.bs.modal hide 方法调用之后立即触发该事件。
hidden.bs.modal 此事件在模态框被隐藏(并且同时在 CSS 过渡效果完成)之后被触发。
loaded.bs.modal 远端的数据源加载完数据之后触发该事件。
 
$('#myModal').on('hidden.bs.modal', function (e) {
// do something...
})

bootstrap modal 垂直居中对齐的更多相关文章

  1. Bootstrap modal垂直居中

    Bootstrap modal垂直居中   在网上看到有Bootstrap2的Modal dialog垂直居中问题解决方法,这种方法自己试了一下,并不能完全居中,并且窗口的大小不一样的话,每次显示的m ...

  2. bootstrap modal垂直居中(简单封装)

    1.使用modal 弹出事件方法: 未封装前: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  3. Bootstrap Modal 垂直居中

    Bootstrap 的 modal 正文中如果内容较少的话,并不会垂直居中,而是偏上, 如果想要达到垂直居中的效果,需要自动动手了. 可以在初始显示时设置垂直居中,可以这样做: $('#YourMod ...

  4. bootstrap modal垂直居中 (转)

    根据博友的经验,总结后请使用方法一就行了 一,修改bootstrap.js 源码 原来的: Modal.prototype.adjustDialog = function () { ].scrollH ...

  5. Bootstrap模态框(modal)垂直居中

    http://v3.bootcss.com/ 自己也试了改了几种方式也不容乐观,发现在窗口弹出之前是获取不到$(this).height()的值,本想着是用($(window).height()-$( ...

  6. Bootstrap(v3.2.0)模态框(modal)垂直居中

    Bootstrap(v3.2.0)模态框(modal)垂直居中方法: 在bootstrap.js文件900行后面添加如下代码,便可以实现垂直居中. that.$element.children().e ...

  7. bootstrap Modal 模态框垂直居中

    解决 Modal 垂直居中的问题,上网找了好多博客,有好多说改源码的,这个并没有实践. 但发现另一种解决办法,可以实现,代码如下: function centerModals(){ $('.modal ...

  8. Bootstrap3模态框Modal垂直居中样式

    1,Bootstrap 模态框插件Bootbox垂直居中样式: <!DOCTYPE html> <html lang="en"> <head> ...

  9. html文本垂直居中对齐

    html文本垂直居中对齐,代码如下: <div id="box" style="height:100px; line-height:100px; border:1p ...

随机推荐

  1. sencha touch list 选择插件,可记忆已选项,可分组全选

    选择记忆功能参考:https://market.sencha.com/extensions/ext-plugin-rememberselection 插件代码: /* * 记住列表选择状态 * 如果分 ...

  2. wireshark和RawCap跟踪并解决中文乱码问题

    一.问题概述 说下程序的架构. 有个后台管理系统A,在页面修改数据后,会用httpClient发http请求给系统B: 系统B做了异步机制,收到A发的请求后,将数据封装为Mq消息发给RabbitMq, ...

  3. Oracle SQL Developer保持数据库连接的方法

    一.概述 从navicat切到pl/sql developer,但是发现个bug,因为本地客户端pl/sql developer的字符集 和数据库服务器的字符集不一致,导致一个很奇葩的东西. 本来我有 ...

  4. 用ChrootDirectory限制SFTP登录的用户只能访问指定目录且不能进行ssh登录

    创建不能ssh登录的用户sftpuser1,密码用于sftp登录: sudo adduser sftpuser1 --home /sftp/sftpuser1 --shell /bin/false s ...

  5. MAC ox下配置mysql

    下载MySQL安装包:http://dev.mysql.com/downloads/mysql/ 如图,选择dmg包,下载完成,双击后打开对应的pkg包,一直点击下一步,直到安装成功 启动和停止MyS ...

  6. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十九:SDRAM模块② — 多字读写

    实验十九:SDRAM模块② — 多字读写 表示19.1 Mode Register的内容. Mode Register A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A ...

  7. Promise在await报错后,如何继续往下跑...

    一.resolve 当a>0时,正常情况依次输出A.B.C console.log("A"); let result = await this.test(); console ...

  8. C# 压缩文件 ICSharpCode.SharpZipLib.dll

    效果: 代码只能压缩文件夹里面的文件,不能压缩文件夹. 压缩前: 压缩后: 代码: 需要引用ICSharpCode.SharpZipLib.dll public ActionResult Index( ...

  9. s3接口认证说明

    S3 Authorization太绕,太头痛,下面解释说明: XS3 REST API基于HMAC(哈希消息身份验证码)密钥使用自定义HTTP方案进行身份验证.要对请求进行身份验证,您首先需要合并请求 ...

  10. [转]-[携程]-A Hybrid Collaborative Filtering Model with Deep Structure for Recommender Systems

    原文链接:推荐系统中基于深度学习的混合协同过滤模型 近些年,深度学习在语音识别.图像处理.自然语言处理等领域都取得了很大的突破与成就.相对来说,深度学习在推荐系统领域的研究与应用还处于早期阶段. 携程 ...