微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我第一次尝试使用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">&times;</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 -->

JSfiddlehttp://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',"-15px");
});

jsFiddle

供参考:

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

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

Reference

原文地址:https://www.jb51.cc/bootstrap/234099.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐