同时隐藏和显示 bootstrap 4 modal 将隐藏浏览器的滚动条

Posted

技术标签:

【中文标题】同时隐藏和显示 bootstrap 4 modal 将隐藏浏览器的滚动条【英文标题】:Hiding and showing bootstrap 4 modal at the same time will hide the browser's scrollbar 【发布时间】:2018-10-31 18:53:29 【问题描述】:

我使用Bootstrap 4.0 modal 来显示内容。第一个模式有点长,但它显示了浏览器的滚动条。在 modal 内部,我有另一个 button 可以调用 2nd modal ,它将隐藏第一个 modal。当我关闭第二个模式时,它会回到第一个模式,但是浏览器的水平滚动条会在 0.5 秒后消失。现在,我无法滚动模式的顶部或底部,因为滚动条已经消失。有没有办法防止滚动条被删除?

【问题讨论】:

发布您的代码.. 【参考方案1】:

我已经尝试过了,请看一下,如果它不起作用,请在下面发表评论,以便我更好地了解问题所在。 它在jsfiddle中有点扭曲,我建议你在codepen中打开 https://codepen.io/anon/pen/OZqxyK

.modal

  overflow:scroll !important;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content" style="overflow:scroll;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
          desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <br><br>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
          desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <br><br>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
          desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <br><br>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
          desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <br><br>
        <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Modal</button>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
<div class="modal fade" id="myModal2" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close2 close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
          It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
          desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        <br><br>
      </div>
      <div class="modal-footer">
        <button type="button" class="close2 btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

【讨论】:

以上是关于同时隐藏和显示 bootstrap 4 modal 将隐藏浏览器的滚动条的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 React-Bootstrap 和 Redux 显示/隐藏 Modal?

通过 MVC 局部视图注入时,Bootstrap 模态显示和隐藏事件未触发

实现bootstrap下的模态窗口功能时手动显示和隐藏窗口报错TypeError $(...).modal is not a function

BootStrap 模态框禁用空白处点击关闭,手动显示隐藏,垂直居中

bootstrap模态框没有显示出来,这是怎么了

python测试开发django-187.Bootstrap模态框(modal)如何在关闭时触发事件