(转)解决bootstrap 模态框的页面抖动

使用bootstrap时,往往会出现页面抖动的效果,使得用户体验十分不友好。

Bootstrap为了让所有的页面(这里指内容溢出和不溢出)显示效果一样,采取的方法如下:

当Modal显示时,设置body -- overflow:hidden;margin-right:15px;(设置15px是因为浏览器的滚动条占位是15px);(通过在modal显示时给body添加.modal-open类实现)

        设置modal -- overflow:auto;overflow-y:scroll;

这样设置的效果是:

  (1)当页面内容超出(即页面本身存在滚动条),则moda弹出后,原body滚动禁止,body的margin-right和modal的滚动条位置重叠,此时页面是不会出现抖动现象的;

  (2)当页面内容未超出(即页面本身不存在滚动条),则modal弹出后,由于body设置了margin-right,会使得页面向左偏移,当modal关闭后,body的margin-right为0,页面向右偏移,就出现页面抖动。

根据上面的描述,解决页面抖动的思路是:

根据scrollHeight和clientHeight,分别在modal加载前和关闭时调整body的overflow、margin-right和.modal的overflow属性,以覆盖bootstrap.css中的样式

函数如下:

(function(){
var Modal = {};
//解决Modal弹出时页面左右移动问题
Modal.adjustBody_beforeShow = function(){
    console.log("1")
var body_scrollHeight = $(‘body‘)[0].scrollHeight;
var docHeight = document.documentElement.clientHeight;
if(body_scrollHeight > docHeight){
  $(‘body‘).css({
    ‘overflow‘ : ‘hidden‘,
    ‘margin-left‘ : ‘17px‘
  });
  $(‘.modal‘).css({‘overflow-y‘:‘scroll‘})
}else{
  $(‘body‘).css({
    ‘overflow‘ : ‘auto‘,
    ‘margin-left‘ : ‘0‘
  });
  $(‘.modal‘).css({‘overflow-y‘:‘auto‘})
}
}
Modal.adjustBody_afterShow = function(){
    console.log("2")
var body_scrollHeight = $(‘body‘)[0].scrollHeight;
var docHeight = document.documentElement.clientHeight;
if(body_scrollHeight > docHeight){
  $(‘body‘).css({
    ‘overflow‘ : ‘auto‘,
    ‘margin-left‘ : ‘0‘
  });
}else{
  $(‘body‘).css({
    ‘overflow‘ : ‘auto‘,
    ‘margin-left‘ : ‘0‘
  });
}
}
$(‘#Xabout‘).modal(‘hide‘);
$(‘#Xabout‘).on(‘show.bs.modal‘, function (event) {
  Modal.adjustBody_beforeShow();
});
$(‘#Xabout‘).on(‘hidden.bs.modal‘, function (event) {
  Modal.adjustBody_afterShow();
});

})();

CSS:body{ overflow: auto !important;} .modal{ overflow: auto !important;}

原文地址1:http://www.cnblogs.com/whimqueen/p/4704919.htm

原文地址2:https://my.oschina.net/deacyn/blog/508249

时间: 2024-08-03 02:32:55

(转)解决bootstrap 模态框的页面抖动的相关文章

Bootstrap3-技巧之解决Bootstrap模态框切换时页面抖动 or页面滚动条

Bootstrap为了让所有的页面(这里指内容溢出和不溢出)显示效果一样,采取的方法如下: 当Modal显示时,设置body -- overflow:hidden;margin-right:15px;(设置15px是因为浏览器的滚动条占位是15px);(通过在modal显示时给body添加.modal-open类实现) 设置modal -- overflow:auto;overflow-y:scroll; 这样设置的效果是: (1)当页面内容超出(即页面本身存在滚动条),则moda弹出后,原bo

解决Bootstrap模态框切换时页面抖动 or页面滚动条

Bootstrap为了让所有的页面(这里指内容溢出和不溢出)显示效果一样,采取的方法如下: 当Modal显示时,设置body -- overflow:hidden;margin-right:15px;(设置15px是因为浏览器的滚动条占位是15px);(通过在modal显示时给body添加.modal-open类实现) 设置modal -- overflow:auto;overflow-y:scroll; 这样设置的效果是: (1)当页面内容超出(即页面本身存在滚动条),则moda弹出后,原bo

Bootstrap 模态框 + iframe > 打开子页面 > 数据传输/关闭模态框

父页面bootstrap模态框: <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" > <div class="modal-dialog modal-lg"> &l

bootstrap模态框远程加载网页的正确处理方式

bootstrap模态框远程加载网页的方法 在bootsrap模态框文档里给出了这个方法: 使用链接模式 <a data-toggle="modal" href="tieniu.php" data-target="#modal">Click me</a> 使用脚本模式: $("#modal").modal({ remote: "tieniu.php" }); 没有给出任何实例,这种用

Bootstrap 模态框(也可以说的弹出层)

最近在尝试使用bootstrap的模态框 使用模态框主要要引入一下几个js和css: bootstrap.css jquery.1.9.1.js(这个可以灵活选择) bootstrap.js html页面的写法如下: <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css"> <script src="bootstrap/js/j

去除BOOTSTRAP模态框半透明阴影

当使用bootstrap模态框默认自带半透明阴影,如果想要去除阴影,需要怎么做呢? 今天在项目中我遇到了这个问题,想要去除模态框的阴影,试了好久都没解决.后来问同事的时候才知道,当模态框弹出后,会加上这样一句代码: <div class="modal-backdrop  in"></div> 案例:自带半透明阴影的模态框 1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta char

关于手动关闭BootStrap模态框

在网上找手动关闭BootStrap模态框的解决方法,说是(需要引用bootstrap.js等): $("#myModal").modal('hide'); 但是我发现我的只能关闭 , 不能关闭下面那半透明层.找了下发现是出现这两行代码的缘故. <div class="modal-backdrop fade in"></div> <div class="modal-backdrop fade in"></d

Bootstrap 模态框、轮播 结合使用

Bootstrap 模态框和轮播分开使用的教程网上很多.但是两者结合使用的例子和资料很少.两者结合使用时,开始我遇到了不少bug,现在分享给大家. 我的这个例子是把图片轮播嵌入到模态框里. 最后的效果如下图:点击页面上按钮,弹出模态框,模态框里的图片开始轮播,点击旁边空白位置,模态框消失. 代码如下: <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 轮播.模态框</title> <link

JavaScript:bootstrap 模态框的简单应用

最近用上了bootstrap这个强大的前端框架,有空来总结一下.这里记录下模态框的简单应用. 首先,要在页面中引入相应的js.css文件 1 <link href="css/bootstrap.css" rel="stylesheet" type="text/css" /> 2 <script type="text/javascript" src="js/jquery.min.js">