日期:2014-05-16  浏览次数:20727 次

jquery 中怎么让一个容器div显示在当前屏幕正中央? - Web 开发 / Ajax
//request data for centering
var windowWidth = document.body.clientWidth;
var windowHeight = document.body.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();
//centering
$("#popupContact").css({
  "position": "absolute",
  "top": windowHeight/2-popupHeight/2,
  "left": windowWidth/2-popupWidth/2
}); 

  通过以上方式,可以达到这一点,不过有个问题,就是如果页面很长,那么这个DIV就不会在显示屏的最中间了,而是显示在顶端的最中间。
  如果要使不管页面有多长,比如点了某个按钮,都会显示在当前显示的最中间?怎么做呢,求解决!

------解决方案--------------------
JScript code

var windowWidth = document.body.scrollWidth;
var windowHeight = document.body.scrollHeight;

------解决方案--------------------
设置它的x y 坐标
------解决方案--------------------
css吧
margin-left:auto;
mrigin-right:auto;
------解决方案--------------------
用滚动条加上页面的位置就可以算出中心

------解决方案--------------------
if( document.documentElement.clientHeight < document.body.scrollHeight ){
height = document.body.scrollHeight;
}else{
height = document.documentElement.clientHeight;
}