因为gpcola的答案对我不起作用,所以我编辑了一下,所以它现在有效。我使用“边距顶部”而不是转换。另外,我使用“show”而不是“show”事件,因为在它给了我一个非常糟糕的定位跳跃之后(当你打开引导动画时可见)。在定位之前,请务必将显示屏设置为“block”,否则$dialog.height() 将为 0,并且模式不会完全居中。
(function ($) {
"use strict";
function centerModal() {
$(this).css('display', 'block');
var $dialog = $(this).find(".modal-dialog"),
offset = ($(window).height() - $dialog.height()) / 2,
bottomMargin = parseInt($dialog.css('marginBottom'), 10);
// Make sure you don't hide the top part of the modal w/ a negative margin if it's longer than the screen height, and keep the margin equal to the bottom margin of the modal
if(offset < bottomMargin) offset = bottomMargin;
$dialog.css("margin-top", offset);
}
$(document).on('show.bs.modal', '.modal', centerModal);
$(window).on("resize", function () {
$('.modal:visible').each(centerModal);
});
}(jQuery));