bootstrap modal模態框垂直居中

直接修改bootstrap.js 源碼:

彈出框出現時, 調用的是 Modal.prototype.show() 方法, 而show 方法中又調用了 that.adjustDialog() 方法:

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 : ''
      })
    }

修改:

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 m_top = ( $(window).height() - $modal_dialog.height() )/2;
        $modal_dialog.css({'margin': m_top + 'px auto'});
    }

 

發佈了75 篇原創文章 · 獲贊 28 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章