Bootstrap modal垂直居中

        在網上看到有Bootstrap2的Modal dialog垂直居中問題解決方法,這種方法本身試了一下,並不能徹底居中,而且窗口的大小不同的話,每次顯示的margin-top值也會改變,遮蓋層還會出現滾動條,效果也很差看,代碼以下:javascript

Js代碼    收藏代碼
  1. //在初始顯示時設置垂直居中  
  2. $('#YourModal').modal().css({  
  3.     'margin-top'function () {  
  4.         return -($(this).height() / 2);  
  5.     }  
  6. });  
  7.   
  8. //或者咱們能夠將這個效果註冊到顯示事件中:  
  9. $('.modal').on('show'function() {  
  10.     $(this).css({  
  11.         'margin-top'function () {  
  12.             return -($(this).height() / 2);  
  13.         }  
  14.     });  
  15. });  

      今天正好遇到這個問題,不過我用的Bootstrap框架是Bootstrap3版本了,解決代碼以下:  css

Js代碼    收藏代碼
  1. $('#YourModal').on('show.bs.modal'function (e) {  
  2.     $(this).find('.modal-dialog').css({  
  3.         'margin-top'function () {  
  4.             var modalHeight = $('#yourModal').find('.modal-dialog').height();  
  5.             return ($(window).height() / 2 - (modalHeight / 2));  
  6.         }  
  7.     });      
  8. });  
     


相關文章
相關標籤/搜索