这篇文章将为大家详细讲解有关怎么设置BootStrap模态框垂直居中,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
解决问题:BootStrap自带的模态框不垂直居中
解决方案:调用BootStrap为我们提供的方法$('.modal').on('show.bs.modal', function(){});
在模态框显示之前我们用JS修改他的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);
关于怎么设置BootStrap模态框垂直居中就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。