bootstrap modal 垂直居中对齐

bootstrap modal 垂直居中对齐

文章参考

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

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

Html代码  

  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样式

Html代码  

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

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

Js代码  

  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. }

解决办法三:修改源代码

Js代码  

  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...
})
时间: 2024-08-27 19:21:07

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

bootstrap modal垂直居中

使用过bootstrap modal(模态框)组件的人都有一种困惑, 好好的一个弹出框怎么就无法垂直居中了呢? 当然网上一些前辈也给出了不少答案, 感觉不太全而且针对的都是各自的项目难以给我等小白太直观的理解.因而手痒试试写个稍微完整点的解决方案, 作为总结及日后回顾之用. 项目中的bootstrap版本是3.X , 作为项目后台使用. 在项目进行过程中遇到组件弹出框无法垂直居中,示例demo代码如下: <!DOCTYPE html> <html> <head> <

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

未封装前: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="css/bootstrap.min.css"> <script src="js/jquery

Bootstrap模态框(modal)垂直居中

http://v3.bootcss.com/ 自己也试了改了几种方式也不容乐观,发现在窗口弹出之前是获取不到$(this).height()的值,本想着是用($(window).height()-$(this).height())/2,发现还是不可行. 最终只能研究一下源码了,发现可以在bootstrap.js文件后面添加如下代码,便可以实现垂直居中. /* =====================================================================

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

Bootstrap(v3.2.0)模态框(modal)垂直居中方法: 在bootstrap.js文件900行后面添加如下代码,便可以实现垂直居中. that.$element.children().eq(0).css("position", "absolute").css({ "margin":"0px", "top": function () { return (that.$element.height(

怎样让一行中的 文字 input输入框 按钮button它们在同一个高度上? 它们中的文字 是 垂直居中对齐

很多时候, 文字和 input和button它们的 顶端是在同一条先上, 但是并不是 垂直居中对齐, 这个 就不好看. 其时有很多理论, 讲的也很复杂, 说什么文本 的top, middle ,baseline , bottom 四根线, 等等 那些我们都不管, 可以这样简单的理解: 把文本/input输入框/button等 都看作是放在 各自一个盒子中的东西, 默认的时候: 当它们在同一行中时, 这些盒子的顶端是对齐的, 即顶端是在同一根线上, 顶端是对齐的, 是盒子不一定是文本顶端对齐; 而

文本如何做到垂直居中对齐

文本如何做到垂直居中对齐:建议:尽可能的手写代码,可以有效的提高学习效率和深度.网页布局中经常用到让文本在一行中垂直居中对齐.默认情况下文本是居上对齐的.实例: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <

随机图片大小在DIV中垂直居中对齐总结

老遇到这种样式 现在总结一下 <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style> *{margin:0;padding:0;line-height: 1;font-size: '宋体';padding

css-实现元素垂直居中对齐

原文:css-实现元素垂直居中对齐 css-实现元素/元素内容,垂直居中对齐 一.单行内容的垂直居中(line-height:行高方法) 只考虑单行是最简单的,无论是否给容器固定高度,只要给容器设置 line-height 和 height,并使两值相等,就可以了. 缺点:1:这种方法局限性太大,只有单行文本的元素才适用,所以在多行元素中是不能使用这种方法的了.         2 :IE中不支持<img>等的居中. 优点:适合在所有浏览器,没有足够空间时,内容不会被切掉,同时支持块级和内联元

bootstrap modal 模态框拖拽扩展

主管要求bootstrap modal 带有拖拽移动效果.代码如下: JS 1 // bootstrap 模态框窗口 移动扩展, 在bootstrap 初始化后 调用 2 var btModalMoveEx = function () { 3 function moveEx($this) { 4 var $head = $this.find(".modal-header"), $dialog = $this.find(".modal-dialog"); 5 var