弹出DIV层 完美 居中js

mac2022-06-30  22

showDiv($("#ShowDiv")); function showDiv(obj){  $(obj).show();  center(obj);  $(window).scroll(function(){   center(obj);  });  $(window).resize(function(){   center(obj);  });  } function center(obj){  var windowWidth = document.documentElement.clientWidth;    var windowHeight = document.documentElement.clientHeight;    var popupHeight = $(obj).height();    var popupWidth = $(obj).width();     $(obj).css({     "position": "absolute",     "top": (windowHeight-popupHeight)/2+$(document).scrollTop(),     "left": (windowWidth-popupWidth)/2    });  }

 

另外div的css 必须包含:

position: absolute;

 

 

然后附上禁用滚动条:

 

var temp_h1 = document.body.clientHeight; var temp_h2 = document.documentElement.clientHeight; var isXhtml = (temp_h2<=temp_h1&&temp_h2!=0)?true:false; var htmlbody = isXhtml?document.documentElement:document.body;

 

 

 

htmlbody.style.overflow = "auto";   //启用

 

htmlbody.style.overflow = "hidden";  //禁用

 

 然后一切ok

 

 

 

据说看了不回帖的人,经常玩 DeBug

转载于:https://www.cnblogs.com/caicainiao/archive/2011/07/05/2098258.html

相关资源:Js制作简单弹出层DIV在页面居中 中间显示遮罩的具体方法
最新回复(0)