设置Bootstrap模式内容以在高度超过浏览器时滚动

Dan*_*ams 3 twitter-bootstrap

我见过很多人问这个问题,但没有真正可靠的答案。

如果模态的高度超过了浏览器的内部高度,我希望红色边框的Div具有滚动条,以将模态保持在浏览器视图中。

#scrollbox {
  border: 1px solid red;
  }
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<input type="button" data-toggle="modal" data-target="#myModal" value="Launch Modal">

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        Hello, below is some text in a div that should start scrolling if the height of the modal exceeds the browser.
        
        <p><div id="scrollbox">
          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>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Kod*_*son 5

对于Bootstrap版本> = 4.3

Bootstrap 4.3为模式添加了新的滚动功能。如果内容的大小会使页面滚动,则仅使模式主体内容滚动。这可能不适用于OP的情况,因为它们在modal-body中还有其他内容,但这可能对其他人有帮助。要使用它,只需将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)


对于<4.3的Bootstrap版本(旧答案)

您可以使用overflow-y:autovh单位执行此操作。将max-height您的属性设置scrollbox100vh(全视口高度),然后减去任意数量的像素以解决scrollbox(要保留在视口内)外部的所有像素。然后添加overflow-y:auto规则,以便如果内容超过max-height,则会出现一个滚动条。除了使用CSS而不是jQuery之外,该答案非常相似。

#scrollbox {
border: 1px solid red;
overflow-y: auto;
max-height: calc(100vh - 150px);
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<input type="button" data-toggle="modal" data-target="#myModal" value="Launch Modal">

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        Hello, below is some text in a div that should start scrolling if the height of the modal exceeds the browser.
        
        <p><div id="scrollbox">
          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>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)