twitter-bootstrap – Bootstrap 3 modal在打开时创建滚动条

栏目: CSS · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/20829332/bootstrap-3-modal-creates-scrollbar-when-opened
我第一次尝试使用Bootstrap,并且在模态对话框中出现问题.使用 this page

上的示例代码,当打开模态时,会出现一个滚动条,这也会将页面上的内容向左移动.

码:

<!-- 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="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</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><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

JSFIDDLE: http://jsfiddle.net/WqRBP/

我正在看的很多网站使用Bootstrap,他们没有这个问题,那么有什么方便的解决这个问题吗?

编辑:滚动条出现在Chrome和IE中,我还没有在其他浏览器中测试过.

以下是我在JSFIDDLE中看到的内容:

发生问题的原因是当启动模态时,Twitter Bootstrap总是将页面15px向左移动.您可以通过将页面移回右侧来解决此问题 – 边距右边:-15px.这可以通过使用Bootstrap的模态提供的事件show.bs.modal和hidden.bs.modal来完成.

$('#myModal').bind('hidden.bs.modal', function () {
  $("html").css("margin-right", "0px");
});
$('#myModal').bind('show.bs.modal', function () {
  $("html").css("margin-right", "-15px");
});

jsFiddle

供参考:

show.bs.modal:当调用show instance方法时,此事件会立即触发.如果由点击导致,点击的元素可用作事件的relatedTarget属性.

hidden.bs.modal:当模态从用户隐藏完成时,会触发此事件(将等待CSS转换完成)​​.

Reference

http://stackoverflow.com/questions/20829332/bootstrap-3-modal-creates-scrollbar-when-opened


以上所述就是小编给大家介绍的《twitter-bootstrap – Bootstrap 3 modal在打开时创建滚动条》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

HTTP Essentials

HTTP Essentials

Stephen A. Thomas、Stephen Thomas / Wiley / 2001-03-08 / USD 34.99

The first complete reference guide to the essential Web protocol As applications and services converge and Web technologies not only assume HTTP but require developers to manipulate it, it is be......一起来看看 《HTTP Essentials》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码