【问题标题】:How to vertically center modal?如何垂直居中模态?
【发布时间】:2017-03-19 00:46:06
【问题描述】:

我想使模态框垂直居中。我在谷歌上搜索了很多,但没有发现任何帮助。这是我的代码:

 function inactive(id, ths) {
   $("#confirmation").modal("show");
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal fade bs-example-modal-sm" id="confirmation" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Are you sure you want to inactivate it?</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <input type="text" class="form-control" name="captcha" id="cval" placeholder="enter captcha">
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
        <button type="button" class="btn btn-primary" id="yes">Yes</button>
      </div>
    </div>
  </div>
</div>

如何垂直居中?

【问题讨论】:

  • 模式出现在屏幕的什么位置?
  • 在顶部。我的导航栏正在覆盖模式。
  • onclick 尝试检查 id

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

如果您在顶部 CSS 属性上使用 CSS calc() 函数,则可以将模态垂直居中。假设模态框的高度为 600px。使用值为 50 的 vh 单位可以获得屏幕垂直高度的中点。然后你只需要减去模态元素高度的一半。

#modal {
   position:  fixed;
   height: 600px;
   top: calc(50vh - 300px);
   z-index: 100;
}

【讨论】:

    【解决方案2】:

    通过添加位置和变换属性,您不会让它在所有宽度和设备上居中,而且一直计算它可能是一个乏味的过程。这是通过设置“.modal-dialog”类的样式来实现的效果

    /* Center a bootstrap modal Vertically and horizontally */
    
    .modal-dialog {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
    }
    
    /* and Viola! */
    

    【讨论】:

      【解决方案3】:

      为此有一个特定的引导类 (v4.1):

      .modal-dialog-centered 添加到 .modal-dialog 以使模式垂直居中。

      来源:https://getbootstrap.com/docs/4.1/components/modal/#vertically-centered

      【讨论】:

        【解决方案4】:

        最简单(未跨浏览器测试但应该没问题):

        HTML:

        <div class="container">
        <p>Container</p>
        <div class="modal">
        <p>Modal</p>
        </div>
        </div>
        

        CSS:

        .container {
        position:relative;
        }
        
        .modal {
        position:absolute;
        top:0;
        bottom:0;
        left:0;
        right:0;
        margin:auto; 
        }
        

        这是小提琴: http://jsfiddle.net/o9pbgnz5/

        同时设置top:0 bottom:0 很重要。然后,margin-top:automargin-bottom:auto 应该注意垂直居中。在此示例中,简写“margin:auto”将其水平和垂直居中。

        附:也适用于position:fixed;

        【讨论】:

          【解决方案5】:

          由于它看起来像一个引导模式,请查看此 Fiddle (jsFiddle)。

          试试

          .modal-dialog {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) !important;
          }
          

          希望这会有所帮助!

          【讨论】:

          • 感谢@D.Malakhov
          【解决方案6】:

          试试这个:-

          // Vertical centered modals
          // you can give custom class like this // var modalVerticalCenterClass = ".modal.modal-vcenter";
          
          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;
          }
          
          /* centering styles for jsbin */
          html,
          body {
            width:100%;
            height:100%;
          }
          html {
            display:table;
          }
          body {
            display:table-cell;
            vertical-align:middle;
          }
          body > .btn {
            display: block;
            margin: 0 auto;
          }
          <!DOCTYPE html>
          <html>
          <head>
              <meta charset="utf-8">
              <title>Bootstrap 3 vertically centered modal and scroll fixes</title>
              <meta name="description" content="Bootstrap 3 vertically centered modal and scroll fixes">
              <!-- include bootstrap -->
              <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
          </head>
          <body>
            
          <!-- Button trigger modal -->
          <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
            Launch modal
          </button>
          
          <!-- Modal -->
          <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
              <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>
                </div>
              </div>
            </div>
          </div>
            
          <!-- include jQuery -->
          <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
            
          <!-- include bootstrap -->
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
            
          </body>
          </html>

          【讨论】:

            【解决方案7】:

            未测试,但你可以试试这个

            .yourElement{
              position: relative;
              top: 50%;
              transform: translateY(-50%);
            }
            

            【讨论】:

            • 如何查看点击了哪个按钮?
            • 如果模态框大于视口高度,则此解决方案将无法正常工作,因为内容将被裁剪。相反,您应该简单地在容器上执行此操作: display: flex;弹性方向:列;证明内容:中心;最小高度:100vh;
            猜你喜欢
            • 2013-10-03
            • 2014-03-07
            • 2016-06-12
            • 1970-01-01
            • 2021-09-17
            • 1970-01-01
            • 2015-03-01
            • 2013-10-15
            • 2014-11-09
            相关资源
            最近更新 更多