bootstrap modal 垂直居中對齊

文章參考javascript

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

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

 

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.   
  19.   
  20. function showTips(){  
  21.     $('#sqh_model').modal('show')  
  22. }  

 

 

默認是距離頂部30px,左右居中java

如圖所示jquery

 

解決辦法一:覆蓋以前的CSS樣式bootstrap

Html代碼   收藏代碼
  1. /**********對bootstrap的modal樣式進行重寫**********/  
  2. .modal-dialog {  
  3.     margin: 200px auto;  
  4. }  

 

 

解決辦法二:調用回調函數api

 

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

 

解決辦法三:修改源代碼app

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

 

 

參數

能夠將選項經過 data 屬性或 javascript 代碼傳遞。對於 data 屬性,須要將參數名稱放到 data- 以後,例如 data-backdrop=""ide

名稱 類型 默認值 描述
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... })
相關文章
相關標籤/搜索