如何在bootstrap模式对话框中仅为模态体添加滚动条

Sub*_*ure 141 css twitter-bootstrap

我有以下元素

<div class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog" style="overflow-y: scroll; max-height:85%;  margin-top: 50px; margin-bottom:50px;" > 
        <div class="modal-content"> 
            <div class="modal-header"> 
                <h3 class="modal-title"></h3> 
            </div> 
            <div class="modal-body"></div> 
            <div class="modal-footer"></div> 
        </div> 
    </div> 
</div> 
Run Code Online (Sandbox Code Playgroud)

它显示这样的模态对话框,基本上它将滚动条放在整个模态对话框周围,而不是包含我试图显示的内容的模态体.

图像看起来像这样 - http://imgur.com/0ZZRjnJ

如何只在模态体周围获得滚动条?我试图分配style ="overflow-y:scroll; max-height:85%; margin-top:50px; margin-bottom:50px;" 模态体,但没有用.

Car*_*lla 253

你必须设置height的的.modal-body中,并给它overflow-y: auto.同时将.modal-dialog溢出值重置为initial.

查看工作示例:

http://www.bootply.com/T0yF2ZNTUd

.modal{
    display: block !important; /* I added this to see the modal, you don't need this */
}

/* Important part */
.modal-dialog{
    overflow-y: initial !important
}
.modal-body{
    height: 250px;
    overflow-y: auto;
}
Run Code Online (Sandbox Code Playgroud)

  • 只是一个小指针:而不是"高度250px",在一些只有230px高度的手机上可能看起来很难看,或者在横向使用导航栏时看起来很简单:只需使用`max-height:80vh;`这是总视口高度的80%仅适用于最大高度,而不是它是一个小弹出窗口 (17认同)
  • @SubodhNijsure对于迟到的回复抱歉.您不能以%为单位设置高度,因为这意味着其父项的百分比,在这种情况下,父项是.modal-content,其高度取决于其子项.这就是为什么除非你设置其中一个高度,否则它将无法工作.您可以设置.modal-content height或设置.modal-body height.如果您设置.modal-content height,那么您将能够以%表示.modal-body的高度,它将是其父级的百分比,因此如果您将其设置为低于100%,则会有空白区域.因为你没有填写所有.modal-content的高度. (3认同)

小智 104

如果您只支持IE 9或更高版本,则可以使用这个可以平滑扩展到窗口大小的CSS.您可能需要调整"200px",具体取决于页眉或页脚的高度.

.modal-body{
    max-height: calc(100vh - 200px);
    overflow-y: auto;
}
Run Code Online (Sandbox Code Playgroud)


小智 27

使用组合解决方案@carlos calla和@jonathan marston解决问题.

    /* Important part */
.modal-dialog{
    overflow-y: initial !important
}
.modal-body{
    max-height: calc(100vh - 200px);
    overflow-y: auto;
}
Run Code Online (Sandbox Code Playgroud)


lau*_*rup 11

再加上Carlos Calla的好回答.

必须设置.modal-body的高度,但您可以使用媒体查询来确保它适合屏幕大小.

.modal-body{
    height: 250px;
    overflow-y: auto;
}

@media (min-height: 500px) {
    .modal-body { height: 400px; }
}

@media (min-height: 800px) {
    .modal-body { height: 600px; }
}
Run Code Online (Sandbox Code Playgroud)

  • @Strawberry 他实际上使用了 min-height(不是 min-width)并确保给定的高度小于媒体查询中的 min-height。此外,他只是举例说明他的想法,因为媒体查询可能会因 OP 想要实现的目标而有很大差异。 (2认同)

dim*_*lmh 10

可选:如果您不希望模态超过窗口高度并使用.modal-body中的滚动条,则可以使用此响应式解决方案.在这里查看工作演示:http://codepen.io/dimbslmh/full/mKfCc/

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() > 767 ? 60 : 20;
  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'));
  }
});
Run Code Online (Sandbox Code Playgroud)


Msk*_*esh 9

在最新的引导版本中,有一个类可以使模态主体可滚动。

.modal-dialog-可滚动.modal-dialog

用于模态主体可滚动内容的 Bootstrap 模态链接

<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
  ...
</div>
Run Code Online (Sandbox Code Playgroud)


Kod*_*son 7

对于Bootstrap版本> = 4.3

Bootstrap 4.3为模态添加了新的内置滚动功能。如果内容的大小会使页面滚动,则仅使模式主体内容滚动。要使用它,只需将modal-dialog-scrollable类添加到具有modal-dialog类的同一div中。

这是一个例子:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalScrollable">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModalScrollable" tabindex="-1" role="dialog" aria-labelledby="exampleModalScrollableTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalScrollableTitle">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">
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
        the<br>quick<br>brown<br>fox<br>
      </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>
Run Code Online (Sandbox Code Playgroud)


小智 6

我知道这是一个古老的话题,但这可能对其他人有所帮助。

我能够通过固定模态对话框元素位置来使主体滚动。因为我永远不会知道浏览器窗口的确切高度,所以我获取了我确定的信息,即页眉和页脚的高度。然后我能够使 modal-body 元素的顶部和底部边距与这些高度相匹配。然后这产生了我正在寻找的结果。我拼凑了一把小提琴来展示我的作品。

另外,如果你想要一个全屏对话框,只需取消注释 width:auto; 在 .modal-dialog.full-screen 部分内。

https://jsfiddle.net/lot224/znrktLej/

这是我用来修改引导对话框的 css。

.modal-dialog.full-screen {
    position:fixed;
    //width:auto;  // uncomment to make the width based on the left/right attributes.
    margin:auto;                        
    left:0px;
    right:0px;
    top:0px;
    bottom:0px;
}

.modal-dialog.full-screen .modal-content {
      position:absolute;
      left:10px;
      right:10px;
      top:10px;
      bottom:10px;
}

.modal-dialog.full-screen .modal-content .modal-header {
        height:55px;  // adjust as needed.
}

.modal-dialog.full-screen .modal-content .modal-body {
        overflow-y: auto;
          position: absolute;
          top: 0;
          bottom: 0;
        left:0;
        right:0;
          margin-top: 55px; // .modal-header height
          margin-bottom: 80px;  // .modal-footer height
}

.modal-dialog.full-screen .modal-content .modal-footer {
        height:80px;  // adjust as needed.
        position:absolute;
        bottom:0;
        left:0;
        right:0;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

或者更简单的是,您可以先将标签放在标签之间,然后再放入 css 类。

<div style="height: 35px;overflow-y: auto;"> Some text o othre div scroll </div>
Run Code Online (Sandbox Code Playgroud)