Css Bootstrap 3模式垂直位置中心

Css Bootstrap 3模式垂直位置中心,css,twitter-bootstrap,modal-dialog,center,Css,Twitter Bootstrap,Modal Dialog,Center,这是一个由两部分组成的问题: 当您不知道模态的确切高度时,如何将模态垂直放置在中心 模态是否可以居中并在模态主体中具有overflow:auto,但前提是模态超过屏幕高度 我试过使用这个: .modal-dialog { height: 80% !important; padding-top:10%; } .modal-content { height: 100% !important; overflow:visible; } .modal-body { height:

这是一个由两部分组成的问题:

  • 当您不知道模态的确切高度时,如何将模态垂直放置在中心

  • 模态是否可以居中并在模态主体中具有overflow:auto,但前提是模态超过屏幕高度

  • 我试过使用这个:

    .modal-dialog {
      height: 80% !important;
      padding-top:10%;
    }
    
    .modal-content {
      height: 100% !important;
      overflow:visible;
    }
    
    .modal-body {
      height: 80%;
      overflow: auto;
    }
    

    当内容比垂直屏幕大得多时,这会给我提供所需的结果,但对于较小的模式内容,它几乎不可用。

    尝试以下方法:

    .popup__overlay {
        position: fixed;
        left:  0;
        top:  0;
        width: 100%;
        height: 100%;
        z-index: 999;
        text-align: center
        }
    .popup {
        display: inline-block;
        vertical-align: middle
        } 
    

    您可能想查看这个div绝对居中的方法集合:

    我想出了一个纯css解决方案!虽然它是css3,这意味着ie8或更低版本不受支持,但除此之外,它已经在ios、android、ie9+、chrome、firefox、桌面safari上进行了测试和工作

    我正在使用以下css:

    .modal-dialog {
      position:absolute;
      top:50% !important;
      transform: translate(0, -50%) !important;
      -ms-transform: translate(0, -50%) !important;
      -webkit-transform: translate(0, -50%) !important;
      margin:auto 5%;
      width:90%;
      height:80%;
    }
    .modal-content {
      min-height:100%;
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      right:0; 
    }
    .modal-body {
      position:absolute;
      top:45px; /** height of header **/
      bottom:45px;  /** height of footer **/
      left:0;
      right:0;
      overflow-y:auto;
    }
    .modal-footer {
      position:absolute;
      bottom:0;
      left:0;
      right:0;
    }
    
    display:table
    
    这是一把小提琴。

    …选择此选项作为正确答案,因为在出现多个模态的情况下,没有额外繁重的javascript会使浏览器崩溃。

    我的解决方案

    .modal-dialog-center {
        margin-top: 25%;
    }
    
        <div id="waitForm" class="modal">
            <div class="modal-dialog modal-dialog-center">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 id="headerBlock" class="modal-title"></h4>
                    </div>
                    <div class="modal-body">
                        <span id="bodyBlock"></span>
                        <br/>
                        <p style="text-align: center">
                            <img src="@Url.Content("~/Content/images/progress-loader.gif")" alt="progress"/>
                        </p>   
                    </div>
                </div>
            </div>
        </div>
    
    .modal对话框中心{
    利润率最高:25%;
    }
    ×
    


    在我的例子中,我所做的就是在css中设置顶部,知道模态的高度

    在我的css中,我设置了

    #myModal{
        height: 400px;
        top: calc(50% - 200px) !important;
    }
    

    我已经从下面的链接下载了bootstrap3对话框,并修改了bootstrap-dialog.js中的open函数

    代码

    open: function () {
                !this.isRealized() && this.realize();
                this.updateClosable();
                //Custom To Vertically centering Bootstrap 
                var $mymodal = this.getModal();
                $mymodal = $mymodal.append('<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td align="center" valign="middle" class="centerModal"></td></tr></table>');
                $mymodal = $mymodal.find(".modal-dialog").appendTo($mymodal.find(".centerModal"));
                //END
                this.getModal().modal('show');
                return this;
            }
    

    1。当您不知道模态的确切高度时,如何将模态垂直放置在中心?

    要在不声明高度的情况下绝对居中引导3模式,首先需要通过将以下内容添加到样式表来覆盖引导CSS:

    .modal-dialog-center { /* Edited classname 10/03/2014 */
        margin: 0;
        position: absolute;
        top: 50%;
        left: 50%;
    }
    
    .modal-body {
        overflow-y: auto;
    }
    .modal-footer {
        margin-top: 0;
    }
    
    这将使模式对话框位于窗口中心的左上角

    我们必须添加此媒体查询,否则在小型设备上留下的模式边距是错误的:

    @media (max-width: 767px) {
      .modal-dialog-center { /* Edited classname 10/03/2014 */
        width: 100%;
      }
    } 
    
    现在我们需要用JavaScript调整它的位置。为此,我们给元素一个负的顶部和左边缘,等于其高度和宽度的一半。在本例中,我们将使用jQuery,因为它可以用于引导

    $('.modal').on('shown.bs.modal', function() {
        $(this).find('.modal-dialog').css({
            'margin-top': function () {
                return -($(this).outerHeight() / 2);
            },
            'margin-left': function () {
                return -($(this).outerWidth() / 2);
            }
        });
    });
    

    更新(2015年10月1日): 加上。归功于

    注意负的边距,对吗?这将删除内联块添加的空间。该空间导致模式跳转到页面底部@media width<768px

    2。模态是否可以居中并在模态正文中显示overflow:auto,但前提是模态超出屏幕高度?

    这可以通过为模态实体提供overflow-y:auto和max height来实现。这需要更多的工作才能让它正常工作。首先将其添加到样式表中:

    .modal-dialog-center { /* Edited classname 10/03/2014 */
        margin: 0;
        position: absolute;
        top: 50%;
        left: 50%;
    }
    
    .modal-body {
        overflow-y: auto;
    }
    .modal-footer {
        margin-top: 0;
    }
    
    我们将再次使用jQuery获取窗口高度,并首先设置模式内容的最大高度。然后我们必须设置模态主体的最大高度,方法是用模态页眉和模态页脚减去模态内容:

    $('.modal').on('shown.bs.modal', function() {
        var contentHeight = $(window).height() - 60;
        var headerHeight = $(this).find('.modal-header').outerHeight() || 2;
        var footerHeight = $(this).find('.modal-footer').outerHeight() || 2;
    
        $(this).find('.modal-content').css({
            'max-height': function () {
                return contentHeight;
            }
        });
    
        $(this).find('.modal-body').css({
            'max-height': function () {
                return (contentHeight - (headerHeight + footerHeight));
            }
        });
    
        $(this).find('.modal-dialog').css({
            'margin-top': function () {
                return -($(this).outerHeight() / 2);
            },
            'margin-left': function () {
                return -($(this).outerWidth() / 2);
            }
        });
    });
    
    您可以在这里找到Bootstrap 3.0.3的工作演示: 编辑:我建议使用更新版本,以获得更具响应性的解决方案:

    更新(30/11/2015):
    函数setModalMaxHeight(元素){
    此.$element=$(element);
    this.$content=this.$element.find('.modal content');
    var borderWidth=this.$content.outerHeight()-this.$content.innerHeight();
    var dialogMargin=$(窗口).width()<768?20:60;
    var contentHeight=$(窗口).height()-(对话框边距+边框宽度);
    var headerHeight=this.$element.find('.modal header').outerHeight()| | 0;
    var footerHeight=this.$element.find('.modal footer').outerHeight()| | 0;
    var maxHeight=内容高度-(页眉高度+页脚高度);
    这是$content.css({
    “溢出”:“隐藏”
    });
    这是$element
    .find('.modal body').css({
    “最大高度”:最大高度,
    “溢出-y”:“自动”
    });
    }
    $('.modal').on('show.bs.modal',function(){
    $(this.show();
    setModalMaxHeight(这个);
    });
    $(窗口)。调整大小(函数(){
    如果($('.modal.in').length!=0){
    setModalMaxHeight($('.modal.in'));
    }
    });
    

    (通过上述编辑于2015年11月30日更新)

    考虑使用此处的引导模式插件-


    这个插件将把你所有的模态集中起来,这是我写的最通用的解决方案。Dynamicly使用对话框高度进行计算。(下一步可能是在调整窗口大小时重新计算对话框的高度。)

    JSfiddle:

    //在文档准备就绪时初始化
    jQuery(文档).ready(函数($){
    "严格使用",;
    //中心情态动词
    //第一阶段-存储每个对话框的高度
    $('.modal')。每个(函数(){
    var t=$(此),
    d=t.find('.modal dialog'),
    fadeClass=(t.is('.fade')?'fade':'';
    //渲染对话框
    t、 removeClass('fade')
    .addClass(“不可见”)
    .css(“显示”、“块”);
    //读取和存储对话框高度
    d、 数据('height',d.height());
    //再次隐藏对话框
    t、 css('显示','')
    .removeClass(“不可见”)
    .addClass(fadeClass);
    });
    //第二阶段-在每次对话框显示时设置页边距顶部
    $('.modal').on('show.bs.modal',函数(){
    var t=$(此),
    d=t.find('.modal dialog'),
    dh=d.数据(“高度”),
    w=$(窗口).width(),
    h=$(窗口).height();
    //如果是桌面,则对话框低于视口(&D)
    //(设置自己的值)
    如果(w>380&(dh+60)
    function setModalMaxHeight(element) {
      this.$element     = $(element);  
      this.$content     = this.$element.find('.modal-content');
      var borderWidth   = this.$content.outerHeight() - this.$content.innerHeight();
      var dialogMargin  = $(window).width() < 768 ? 20 : 60;
      var contentHeight = $(window).height() - (dialogMargin + borderWidth);
      var headerHeight  = this.$element.find('.modal-header').outerHeight() || 0;
      var footerHeight  = this.$element.find('.modal-footer').outerHeight() || 0;
      var maxHeight     = contentHeight - (headerHeight + footerHeight);
    
      this.$content.css({
          'overflow': 'hidden'
      });
    
      this.$element
        .find('.modal-body').css({
          'max-height': maxHeight,
          'overflow-y': 'auto'
      });
    }
    
    $('.modal').on('show.bs.modal', function() {
      $(this).show();
      setModalMaxHeight(this);
    });
    
    $(window).resize(function() {
      if ($('.modal.in').length != 0) {
        setModalMaxHeight($('.modal.in'));
      }
    });
    
    // initialise on document ready
    jQuery(document).ready(function ($) {
        'use strict';
    
        // CENTERED MODALS
        // phase one - store every dialog's height
        $('.modal').each(function () {
            var t = $(this),
                d = t.find('.modal-dialog'),
                fadeClass = (t.is('.fade') ? 'fade' : '');
            // render dialog
            t.removeClass('fade')
                .addClass('invisible')
                .css('display', 'block');
            // read and store dialog height
            d.data('height', d.height());
            // hide dialog again
            t.css('display', '')
                .removeClass('invisible')
                .addClass(fadeClass);
        });
        // phase two - set margin-top on every dialog show
        $('.modal').on('show.bs.modal', function () {
            var t = $(this),
                d = t.find('.modal-dialog'),
                dh = d.data('height'),
                w = $(window).width(),
                h = $(window).height();
            // if it is desktop & dialog is lower than viewport
            // (set your own values)
            if (w > 380 && (dh + 60) < h) {
                d.css('margin-top', Math.round(0.96 * (h - dh) / 2));
            } else {
                d.css('margin-top', '');
            }
        });
    
    });
    
    .modal {
      text-align: center;
    }
    
    @media screen and (min-width: 768px) { 
      .modal:before {
        display: inline-block;
        vertical-align: middle;
        content: " ";
        height: 100%;
      }
    }
    
    .modal-dialog {
      display: inline-block;
      text-align: left;
      vertical-align: middle;
    }
    
    $('#myModal').on('loaded.bs.modal', function() {
        $(this).find('.modal-dialog').css({
            'margin-top': function () {
                return (($(window).outerHeight() / 2) - ($(this).outerHeight() / 2));
            }
        });
    });
    
    display:table
    
    .modal-dialog {
      height: 100%;
      width: 100%;
      display: flex;
      align-items: center;
    }
    
    .modal-content {
      margin: 0 auto;
    }
    
    $(document).ready(function(){
        var modalId = "#myModal";
        resize: function(){
                var new_margin = Math.ceil(($(window).height() - $(modalId).find('.modal-dialog').height()) / 2);
                $(modalId).find('.modal-dialog').css('margin-top', new_margin + 'px');
        }
        $(window).resize(function(){
            resize();
        });
        $(modalId).on('shown.bs.modal', function(){
            resize();
        });
    });
    
    .modal {
      text-align: center;
    }
    @media screen and (min-device-width: 768px) {
      .modal:before {
        display: inline-block;
        vertical-align: middle;
        content: " ";
        height: 100%;
      }
    }
    
    .modal-dialog {
      display: inline-block;
      text-align: left;
      vertical-align: middle;
    }
    
    .modal-dialog {
      margin-top: 0;
      margin-bottom: 0;
      height: 100vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    
    .modal.fade .modal-dialog {
      transform: translate(0, -100%);
    }
    
    .modal.in .modal-dialog {
      transform: translate(0, 0);
    }
    
    .modal-dialog {
      margin-top: 0;
      margin-bottom: 0;
      height: 100vh;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -webkit-flex-direction: column;
          -ms-flex-direction: column;
              flex-direction: column;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
          -ms-flex-pack: center;
              justify-content: center;
    }
    
    .modal.fade .modal-dialog {
      -webkit-transform: translate(0, -100%);
              transform: translate(0, -100%);
    }
    .modal.in .modal-dialog {
      -webkit-transform: translate(0, 0);
              transform: translate(0, 0);
    }
    
    <style>
    .vertical-alignment-helper {
        display:table;
        height: 100%;
        width: 100%;
    }
    .vertical-align-center {
        /* To center vertically */
        display: table-cell;
        vertical-align: middle;
    }
    .modal-content {
        /* Bootstrap sets the size of the modal in the modal-dialog class, we need to inherit it */
        width:inherit;
        height:inherit;
        /* To center horizontally */
        margin: 0 auto;
    }
    </style>
    <!-- Button trigger modal -->
    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="vertical-alignment-helper">
            <div class="modal-dialog vertical-align-center">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
    
                        </button>
                         <h4 class="modal-title" id="myModalLabel">Modal title</h4>
    
                    </div>
                    <div class="modal-body">...</div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
            </div>
        </div>
    </div>    
    
    .modal-dialog {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        width:500px;
        height:300px;
    }
    
    var modalVerticalCenterClass = ".modal";
    
    function centerModals($element) {
        var $modals;
        if ($element.length) {
        $modals = $element;
        } else {
        $modals = $(modalVerticalCenterClass + ':visible');
    }
    $modals.each( function(i) {
        var $clone = $(this).clone().css('display', 'block').appendTo('body');
        var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
        top = top > 0 ? top : 0;
        $clone.remove();
        $(this).find('.modal-content').css("margin-top", top);
        });
    }
    $(modalVerticalCenterClass).on('show.bs.modal', function(e) {
        centerModals($(this));
    });
    $(window).on('resize', centerModals);
    
    /* scroll fixes */
    .modal-open .modal {
        padding-left: 0px !important;
        padding-right: 0px !important;
        overflow-y: scroll;
    }
    
    (function ($) {
        "use strict";
        function centerModal() {
            $(this).css('display', 'block');
            var $dialog  = $(this).find(".modal-dialog"),
                offset       = ($(window).height() - $dialog.height()) / 2,
                bottomMargin = parseInt($dialog.css('marginBottom'), 10);
    
            // Make sure you don't hide the top part of the modal w/ a negative margin if it's longer than the screen height, and keep the margin equal to the bottom margin of the modal
            if(offset < bottomMargin) offset = bottomMargin;
            $dialog.css("margin-top", offset);
        }
    
        $(document).on('show.bs.modal', '.modal', centerModal);
        $(window).on("resize", function () {
            $('.modal:visible').each(centerModal);
    
        });
    })(jQuery);
    
    .modal {
        height: 100%;
    
        .modal-dialog {
            top: 50% !important;
            margin-top:0;
            margin-bottom:0;
        }
    
        //keep proper transitions on fade in
        &.fade .modal-dialog {
            transform: translateY(-100%) !important;
        }
        &.in .modal-dialog {
            transform: translateY(-50%) !important;
        }
    }
    
    <div class="modal-container">
      <style>
      .modal-dialog{
        margin-top: 60%;
        width:80%;
        margin-left: 10%;
        margin-right: 10%;
        margin-bottom: 100%
      }
      @media screen and (orientation:landscape){
        .modal-dialog{
          margin-top: 70;
          width:80%;
          margin-left: 10%;
          margin-right: 10%;
          margin-bottom: 100%
        }
      }
      .modal-body{
        text-align: center;
      }
      .modal-body p{
        margin:14px 0px;
        font-size: 110%;
      }
      .modal-content{
        border-radius: 10px;
      }
      .modal-footer{
        padding:0px;
      }
      .modal-footer a{
        padding: 15px;
      }
      .modal-footer a:nth-child(1){
        border-radius: 0px 0px 0px 10px;
      }
      .modal-footer a:nth-child(2){
        border-radius: 0px 0px 10px 0px;
      }
      </style>
      <h2>Basic Modal Example</h2>
      <div data-toggle="modal" data-target="#myModal">Div for modal</div>
        <div class="modal fade" id="myModal" role="dialog">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-body">
                <p>确定要取消本次订单嘛?</p>
              </div>
              <div class="modal-footer">
                <div class="btn-group btn-group-justified">
                  <a href="#" class="btn btn-default" data-dismiss="modal">取消</a>
                  <a href="#" class="btn btn-default" data-dismiss="modal">确定</a>
                </div>
              </div>
            </div>
          </div>
        </div>
    </div>
    
    $(function() {
        function reposition() {
            var modal = $(this),
                dialog = modal.find('.modal-dialog');
            modal.css('display', 'block');
    
            // Dividing by two centers the modal exactly, but dividing by three 
            // or four works better for larger screens.
            dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));
        }
        // Reposition when a modal is shown
        $('.modal').on('show.bs.modal', reposition);
        // Reposition when the window is resized
        $(window).on('resize', function() {
            $('.modal:visible').each(reposition);
        });
    });
    
    var modalVerticalCenterClass = ".modal";
    function centerModals($element) {
        var $modals;
        if ($element.length) {
            $modals = $element;
        } else {
            $modals = $(modalVerticalCenterClass + ':visible');
        }
        $modals.each( function(i) {
            var $clone = $(this).clone().css('display', 'block').appendTo('body');
            var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
            top = top > 0 ? top : 0;
            $clone.remove();
            $(this).find('.modal-content').css("margin-top", top);
        });
    }
    $(modalVerticalCenterClass).on('show.bs.modal', function(e) {
        centerModals($(this));
    });
    $(window).on('resize', centerModals);
    
    .modal-dialog-wrap {
      display: table;
      table-layout: fixed;
      width: 100%;
      height: 100%;
    }
    
    .modal-dialog {
      display: table-cell;
      vertical-align: middle;
      text-align: center;
    }
    
    .modal-content {
      display: inline-block;
      text-align: left;
    }
    
    .modal {
      text-align: center;
      padding: 0!important;
    }
    
    .modal:before {
      content: '';
      display: inline-block;
      height: 100%;
      vertical-align: middle;
      margin-right: -4px;
    }
    
    .modal-dialog {
      display: inline-block;
      text-align: left;
      vertical-align: middle;
    }
    
    .modal.in .modal-dialog 
    {
        -webkit-transform: translate(0, calc(50vh - 50%));
        -ms-transform: translate(0, 50vh) translate(0, -50%);
        -o-transform: translate(0, calc(50vh - 50%));
        transform: translate(0, 50vh) translate(0, -50%);
    }
    
    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
      Launch demo modal
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    
    .modal-dialog {
      height: 100vh !important;
      display: flex;
    }
    
    .modal-content {
      margin: auto !important;
      height: fit-content !important;
    }