在网上看到有Bootstrap2的Modal dialog垂直居中问题解决方法,这种方法本身试了一下,并不能彻底居中,而且窗口的大小不同的话,每次显示的margin-top值也会改变,遮盖层还会出现滚动条,效果也很差看,代码以下:javascript
- //在初始显示时设置垂直居中
- $('#YourModal').modal().css({
- 'margin-top': function () {
- return -($(this).height() / 2);
- }
- });
- //或者咱们能够将这个效果注册到显示事件中:
- $('.modal').on('show', function() {
- $(this).css({
- 'margin-top': function () {
- return -($(this).height() / 2);
- }
- });
- });
今天正好遇到这个问题,不过我用的Bootstrap框架是Bootstrap3版本了,解决代码以下: css
- $('#YourModal').on('show.bs.modal', function (e) {
- $(this).find('.modal-dialog').css({
- 'margin-top': function () {
- var modalHeight = $('#yourModal').find('.modal-dialog').height();
- return ($(window).height() / 2 - (modalHeight / 2));
- }
- });
- });