我想让我的bootstrap模式出现后在页面上水平居中,但我似乎只能让代码垂直居中。这是我目前的代码。
$(document).ready(function () {
function alignModal() {
var modalDialog = $(this).find(".modal-dialog");
// Applying the top margin on modal dialog to align it vertically center
modalDialog.css("margin-top", Math.max(0, ($(window).height() - modalDialog.height()) / 2));
}
// Align modal when it is displayed
$(".modal").on("shown.bs.modal", alignModal);
// Align modal when user resize the window
$(window).on("resize", function () {
$(".modal:visible").each(alignModal);
});
});
你的意思是居中 纵向? 因为你写了两次 "横向"。如果是竖着写。从 Bootstrap的文档页 :
增加 .modal-dialog-centered 到.modal-dialog,使modal垂直居中。