Bootstrap4模态框垂直居中

Bootstrap4模态框垂直居中,只需要在添加.modal-dialog类的div再添加一个.modal-dialog-centered就可以了。具体如下。

Bootstrap4模态框默认不会居中显示在浏览器窗口中心而是显示在窗口顶部,在很多情况下这种设计十分不美观。

百度的时候发现能找到的资料都是Bootstrap3的,而这些方法在Bootstrap4上大部分已经不适用了。

在这里先直接给出Bootstrap4上模态框居中的解决方案,后面再补充一些Bootstrap3的方案(自己并没有在Bootstrap3上尝试过)以及自己在Bootstrap4尝试这些方案的时候遇到的部分问题,以供参考。

额,说来惭愧。。。解决方案非常简单,官方文档写的清清楚楚,但是之前就是没有注意到。。。

只需要在添加.modal-dialog类的div再添加一个.modal-dialog-centered就可以了。官方示例代码效果如图,自己测试也完全没有任何问题。

失败方案:

  • .align-items-center,align-self-center,align-content-center等类可以使元素数值居中,但是对模态框没有效果。
  • 使用js调整模态框margin-top属性使其位于屏幕中心。margin-top = (浏览器窗口高度 -  模态框高度) / 2。
    function util_set_modal_position() {
        let modal = $("#modal-id");
        let margin_top = ($(window).height() - modal.height()) / 2 + "px";
        //下面三行删除
        alert($(window).height());
        alert(modal.height());
        alert(margin_top);
        //上面三行删除
        modal.css("margin-top", margin_top);
    }

    发现没有效果,百度发现模态框弹出之前是获取不到高度的,实验发现在Bootstrap4中即使模态框弹出也无法获取高度。。。alert(modal.height())的值为0或者一个很小的负数,暂时还不清楚是什么。

  • 修改bootstrap.js源代码,这是原文链接,这里是Bootstrap3的源代码,Bootstrap4和它有些差异(对应代码2422行左右的位置),自己只是个前端菜狗,JavaScript都没怎么学过,自己改起来有点费劲,索性懒得改了,大佬们可以自己改一下。这种修改源代码的方法还有一个弊端就是不能使用CDN导入Bootstrap。
  • 其他失败方案就不写了,方正都是失败的,也没卵用。。。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个基于Bootstrap 4的模态框弹出效果的示例代码: HTML代码: ```html <!-- 模态框弹出按钮 --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"> 弹出模态框 </button> <!-- 模态框弹出窗口 --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="myModalLabel">模态框标题</h5> <button type="button" class="close" data-dismiss="modal" aria-label="关闭"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <p>这里是模态框的内容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary">保存</button> </div> </div> </div> </div> ``` CSS代码: ```css /* 设置模态框弹出窗口的样式 */ .modal-dialog { max-width: 600px; } /* 设置模态框标题的样式 */ .modal-header { background-color: #007bff; color: #fff; } /* 设置模态框关闭按钮的样式 */ .modal-header .close { color: #fff; } /* 设置模态框保存按钮的样式 */ .modal-footer .btn-primary { background-color: #007bff; border-color: #007bff; } /* 设置模态框关闭按钮和保存按钮之间的间距 */ .modal-footer .btn-secondary { margin-right: 10px; } ``` JavaScript代码: ```javascript // 初始化模态框 $('#myModal').modal({ backdrop: 'static', keyboard: false, show: false }); ``` 这段代码中,我们使用了Bootstrap 4的模态框组件来实现弹出效果。我们首先创建了一个按钮,设置了data-toggle和data-target属性,这两个属性用于触发模态框的弹出。然后,我们创建了一个模态框的弹出窗口,设置了id属性和aria-labelledby属性,这两个属性用于指定模态框的唯一标识和标题。在模态框的弹出窗口中,我们分别创建了模态框的标题、内容和底部按钮。最后,我们使用JavaScript代码来初始化模态框,设置了backdrop、keyboard和show属性,这些属性用于控制模态框的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值