bootstrap 模态框默认会在屏幕顶端显示,跟我们的操作习惯不太符合,我们期望显示到屏幕垂直居中的位置。
在最新的 bootstrap4 版本中可以在<div class="modal-dialog">
上加一个 modal-dialog-centered
属性,来垂直居中显示。
但是大部分用的是bootstrap3 版本,需要自己写javascript脚本来调整位置。
bootstrap模态框默认会在屏幕顶端显示,位置太靠上了。
bootstrap3 版本,需要自己写 javascript 脚本来调整位置。
// 作者-上海悠悠 QQ交流群:717225969
// blog地址 https://www.cnblogs.com/yoyoketang/
<script>
$('#myModal').on('show.bs.modal', function (e) {
var modalHeight=$(window).height() / 2 - $('#myModal .modal-dialog').height() / 2;
$(this).find('.modal-dialog').css({
'margin-top': modalHeight
});
});
</script>
实现效果
如果想显示在屏幕正中间往上一点的位置,可以通过modalHeight的值控制,比如正中间往上100个像素
var modalHeight=$(window).height() / 2 - $('#myModal .modal-dialog').height() / 2 - 100;
bootstrap4 版本可以在<div class="modal-dialog">
上加一个 modal-dialog-centered
属性,来垂直居中显示。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有