当前位置: 移动技术网 > IT编程>开发语言>JavaScript > BootStrap模态框不垂直居中的解决方法

BootStrap模态框不垂直居中的解决方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了bootstrap模态框不垂直居中的解决方法,供大家参考,具体内容如下

解决问题:bootstrap自带的模态框不垂直居中

解决方案:调用bootstrap为我们提供的方法$('.modal').on('show.bs.modal', function(){});

在模态框显示之前我们用js修改他的top值,

具体代码如下:

/** 
 * 垂直居中模态框 
 **/ 
function centermodals() { 
  $('.modal').each(function(i) { 
    var $clone = $(this).clone().css('display', 'block').appendto('body'); 
    var top = math.round(($clone.height() - $clone.find('.modal-content').height()) / 2); 
    top = top > 50 ? top : 0; 
    $clone.remove(); 
    $(this).find('.modal-content').css("margin-top", top - 50); 
  }); 
} 
// 在模态框出现的时候调用垂直居中方法 
$('.modal').on('show.bs.modal', centermodals); 
// 在窗口大小改变的时候调用垂直居中方法 
$(window).on('resize', centermodals); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网