弹出DIV层 完美 居中js
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;
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