如何将引导模式垂直居中?我在这里寻找解决方案,但他们没有回应,或者根本就没有工作。我正在使用Bootsrap 3。
对于较小的屏幕,或者在使用下面的解决方案后调整浏览器窗口大小时,模式不再响应。
jquery的,
$('.modal').on('shown.bs.modal', function() {
$(this).find('.modal-dialog').css({
'margin-top': function () {
return -($(this).outerHeight() / 2);
},
'margin-left': function () {
return -($(this).outerWidth() / 2);
}
});
});
的CSS,
.modal-dialog {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
}
自举,
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="false">
<div class="modal-dialog custom-class">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
有什么想法吗?
修改
我试过这个css,
@media screen and (min-width: 768px) {
.custom-class {
width: 800px; /* either % (e.g. 60%) or px (400px) */
top: 25%;
}
}
模态只会回落到较小屏幕上的默认位置,但我希望它垂直居中。
同样,top: 25%;
并未使其完全垂直居中。
答案 0 :(得分:2)
我认为就是这样,
的CSS,
.modal-dialog {
top: 0;
margin-top:0;
display: none;
border:1px solid red;
}
jquery + bootstrap,
$('#myModal').on('shown.bs.modal', function (e) {
var object = $('.modal-dialog');
var object_outerheight = object.outerHeight();
var window_height = $(window).height();
var window_scrolltop = $(window).scrollTop();
var object_top = ((window_height - object_outerheight)/2) + window_scrolltop;
console.log(object_top);
// Set the object's position.
object.css({
marginTop: object_top + 'px'
}).fadeIn();
// Add responsive when the window is being resized.
$( window ).resize(function() {
// Redo the calc on each resize's action.
var window_height = $(window).height();
var window_scrolltop = $(window).scrollTop();
var object_top = ((window_height - object_outerheight)/2) + window_scrolltop;
console.log(object_top);
// Reset the object's position.
object.css({
marginTop: object_top + 'px'
});
});
});
答案 1 :(得分:1)
使用css我们可以将Bootstrap模态放在垂直中心。
.modal {
text-align: center;
padding: 0!important;
}
.modal:before {
content: '';
display: inline-block;
vertical-align: middle;
margin: -2px;
height: 100%;
}
.modal-dialog {
display: inline-block;
vertical-align: middle;
}