居中显示bootstrap modal

在使用 bootstrap modal 的时候, 默认的样式不是居中遮罩显示的,看着很别扭 而且还不想去修改它自身的代码,就在网上查了查, 看到这篇博客的方法还是很不错,转载记录于此 http://www.abeautifulsite.net/vertically-centering-bootstrap-modals/

只需要在你的js代码里面加入这样几个函数就可以,很清晰的code .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//最好在jquey的环境下
function reposition() {
var modal = $(this),
dialog = modal.find('.modal-dialog');
modal.css('display', 'block');

// Dividing by two centers the modal exactly, but dividing by three
// or four works better for larger screens.
dialog.css("margin-top", Math.max(0, ($(window).height() - dialog.height()) / 2));
}
// Reposition when a modal is shown
$('.modal').on('show.bs.modal', reposition);
// Reposition when the window is resized
$(window).on('resize', function() {
$('.modal:visible').each(reposition);
});