在网上找了不少方法,如下面两个,自己写方法修改,并不能很好的解决
https://blog.csdn.net/altaba/article/details/54669660 其中的第二种方法在页面第一次加载之后弹出模态框还是会出现闪动,之后就不会了
https://www.cnblogs.com/web-wangmeng/p/5616174.html
直到找到 https://www.jb51.net/article/126219.htm
直接调用调用Boostrap提供的$(’.modal’).on(‘show.bs.modal’, function(){})方法
在模态框出现前修改Top值,也不需要修改源码
function centerModals() {
$('.modal').each(function(i) {
var $clone = $(this).clone().css('display', 'block').appendTo('body');
var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
top = top > 50 ? top : 0;
$clone.remove();
$(this).find('.modal-content').css("margin-top", top - 50);
});
}
// 在模态框出现的时候调用垂直居中方法
$('.modal').on('show.bs.modal', centerModals);
// 在窗口大小改变的时候调用垂直居中方法
$(window).on('resize', centerModals);