设置居中

来源:互联网 发布:chrome 全屏 mac 编辑:程序博客网 时间:2024/05/01 00:52

function setDivCenter(obj){
 //Dialog对象宽高
 var objwh = {w:0,h:0};
 objwh.h = $(obj).height();
 objwh.w = $(obj).width();
 //Window宽高
 var winwh = {w:0,h:0};
 winwh.h = getWindowHeight();
 winwh.w = getWindowWidth();
 //当前滚动到的位置
 var scrollPos = {t:0,l:0};
 scrollPos.t = $(document).scrollTop();
 scrollPos.l = $(document).scrollLeft();
    //Dialog显示位置
 var position = {t:0,l:0};
 position.t = scrollPos.t+((winwh.h-objwh.h)/2);
 position.l = scrollPos.l+((winwh.w-objwh.w)/2);
 position.t = position.t<=0?0:position.t;
 position.l = position.l<=0?0:position.l;
 //移动Dialog居中
 $(obj).css("top",position.t);
 $(obj).css("left",position.l);
 $(obj).css("height","auto");
 //内部函数
 //获取浏览器高
 function getWindowHeight(){
  if($.browser.msie){
   return document.compatMode == "CSS1Compat"? document.documentElement.clientHeight :
      document.body.clientHeight;
  }else{
   return self.innerHeight;
  }
 }
 //获取浏览器宽
 function getWindowWidth (){
  if($.browser.msie){
   return document.compatMode == "CSS1Compat"? document.documentElement.clientWidth :
      document.body.clientWidth;
  }else{
   return self.innerWidth;
  }
 }
}