Bootstrap modal垂直居中

        在网上看到有Bootstrap2的Modal dialog垂直居中问题解决方法,这种方法自己试了一下,并不能完全居中,并且窗口的大小不一样的话,每次显示的margin-top值也会改变,遮盖层还会出现滚动条,效果也不好看,代码如下:

//在初始显示时设置垂直居中
$('#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版本了,解决代码如下:  

$('#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));
        }
    });    
});

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值