提问人:Nate 提问时间:12/30/2013 最后编辑:Mike CauserNate 更新时间:8/5/2019 访问量:33704
Bootstrap 3 模式在打开时创建滚动条
Bootstrap 3 modal creates scrollbar when opened
问:
我第一次尝试使用 Bootstrap,但遇到了模式对话框问题。使用此页面上的示例代码,当打开模式时,将显示一个滚动条,该滚动条也会将页面上的内容向左移动。
法典:
<!-- 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 中看到的:
答:
出现此问题的原因是,打开模态时,始终将页面向左移动 15px。您可以通过将页面移回右侧 - 来解决此问题。这可以通过使用事件和 提供来完成。Twitter Bootstrap
margin-right: -15px
show.bs.modal
hidden.bs.modal
Bootstrap's modal
$('#myModal').bind('hidden.bs.modal', function () {
$("html").css("margin-right", "0px");
});
$('#myModal').bind('show.bs.modal', function () {
$("html").css("margin-right", "-15px");
});
仅供参考:
show.bs.modal
:调用 show 实例方法时,此事件立即触发。如果由单击引起,则单击的元素可用作事件的 relatedTarget 属性。
hidden.bs.modal
:当模态完成对用户的隐藏时,将触发此事件(将等待 CSS 转换完成)。
评论
LVarayut 的回答让我朝着正确的方向前进,我最终使用的是:
body.modal-open, .modal-open .navbar-fixed-top, .modal-open .navbar-fixed-bottom {
margin-right: 0;
}
.modal {
overflow-y: auto;
}
评论
margin-right: 0
overflow-y: auto;
这是我的解决方案
#myModal{
overflow:hidden;
}
body {
overflow-y: scroll !important;
}
有了这个,您将强制您的页面有一个滚动条。
评论
此问题已在 2014 年 6 月 23 日发布的 Bootstrap 版本 3.2.0 中得到解决。
感谢 @roadsunknown 提供的链接(在问题评论中)。
评论
style="padding-right: 16px;"
.modal-open { overflow-y:auto }
尝试使用以下 css :
.modal{
overflow:auto;
}
我已经通过这种方式解决了我的问题。
这对我来说是个把戏
$('html').bind('hidden.bs.modal', function () {
console.log('hidden');
$("html").css("margin-right", "0px");
});
$('html').bind('hide.bs.modal', function () {
console.log('hiding');
$("html").css("margin-right", "-15px");
});
$('html').bind('show.bs.modal', function () {
console.log('shown');
$("html").css("margin-right", "-15px");
});
这是我的解决方案:
.modal {
margin-right: -15px;
}`
并添加此内容
body.modal-open {
margin-right: 0 !important;
}
希望这对你有所帮助。
就是这么简单,只需添加到你的css中:
body.modal-open{overflow:hidden!important;}
/*Optional:*/
.modal-open, .modal{padding-right:0!important}
这主要是由于使用的CDN...从您的页面中删除 CDN https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js“,问题就解决了。我已经做到了。
这对我有用。它应该工作正常
body.modal-open {
padding-right: 0 !important;
overflow-y: scroll;
}
在这种情况下,您可以使用自己的 css 覆盖引导主 css,因为有时可能会影响其他。所以这是简单的代码,可以工作
body.modal-open .modal {
margin-right: -15px !important;
}
body.modal-open {
margin-right: 0 !important;
}
如果要更改主引导 CSS,请更新此内容
body.modal-open, .modal-open .navbar-fixed-top, .modal-open .navbar-fixed-bottom {
margin-right: 0 !important;
}
试试这个
.modal-open {
overflow-y: auto
}
就我而言,是我自己的风格导致了这个问题:
html {
overflow-y: scroll;
}
您可以更改为只有一个滚动条:html
body
body {
overflow-y: scroll;
}
上一个:将左连接限制为返回一个结果?
下一个:如何在MySQL中选择非空列?
评论