• bootstrap modal垂直居中 (转)


     根据博友的经验,总结后请使用方法一就行了

    一,修改bootstrap.js 源码

    原来的:

      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;//浏览器高 - 模态框高 再 / 2
        $modal_dialog.css({'margin': m_top + 'px auto'});
      }

    如果垂直居中了

    二,jquery中

    <script type="text/javascript">
          $(function(){
            // dom加载完毕
            var $m_btn = $('#modalBtn');
            var $modal = $('#myModal');
            $m_btn.on('click', function(){
              $modal.modal({backdrop: 'static'});
            });
    
            // 测试 bootstrap 居中
            $modal.on('show.bs.modal', function(){
              var $this = $(this);
              var $modal_dialog = $this.find('.modal-dialog');
              // 关键代码,如没将modal设置为 block,则$modala_dialog.height() 为零
              $this.css('display', 'block');
              $modal_dialog.css({'margin-top': Math.max(0, ($(window).height() - $modal_dialog.height()) / 2) });
            });
            
          });
        </script>

     这里参考这位博友吧:http://www.cnblogs.com/zzj-suxiao/articles/5460972.html

  • 相关阅读:
    第二次会议记录(2021.7.19)
    第一次会议记录(2021.7.8)
    软件工程助教工作总结
    Linux下的常用命令
    GPIO输出——使用固件库点亮LED 宏定义遇到的问题
    STM32 GPIO BRR和BSRR寄存器
    snprintf()函数使用方法
    结构体元素偏移量宏的定义及解析
    函数指针&回调函数Callback
    解析#define NULL ((void *)0)——野指针,空指针和 void*
  • 原文地址:https://www.cnblogs.com/xiangsj/p/6419743.html
Copyright © 2020-2023  润新知