JS获取页面实际大小函数

Lightbox里面的一个函数,能把页面实际的高宽与浏览器可视面积的高宽保存在一个数组中..非常好用.记录一下

  1. function getPageSize(){
  2.    
  3.     var xScroll, yScroll;
  4.    
  5.     if (window.innerHeight && window.scrollMaxY) {   
  6.         xScroll = document.body.scrollWidth;
  7.         yScroll = window.innerHeight + window.scrollMaxY;
  8.     } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  9.         xScroll = document.body.scrollWidth;
  10.         yScroll = document.body.scrollHeight;
  11.     } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  12.         xScroll = document.body.offsetWidth;
  13.         yScroll = document.body.offsetHeight;
  14.     }
  15.    
  16.     var windowWidth, windowHeight;
  17.     if (self.innerHeight) {    // all except Explorer
  18.         windowWidth = self.innerWidth;
  19.         windowHeight = self.innerHeight;
  20.     } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  21.         windowWidth = document.documentElement.clientWidth;
  22.         windowHeight = document.documentElement.clientHeight;
  23.     } else if (document.body) { // other Explorers
  24.         windowWidth = document.body.clientWidth;
  25.         windowHeight = document.body.clientHeight;
  26.     }   
  27.    
  28.     // for small pages with total height less then height of the viewport
  29.     if(yScroll < windowHeight){
  30.         pageHeight = windowHeight;
  31.     } else { 
  32.         pageHeight = yScroll;
  33.     }
  34.  
  35.     // for small pages with total width less then width of the viewport
  36.     if(xScroll < windowWidth){   
  37.         pageWidth = windowWidth;
  38.     } else {
  39.         pageWidth = xScroll;
  40.     }
  41.  
  42.     arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  43.     return arrayPageSize;
  44. }

调用:

  1. var getPageSize = getPageSize();
  2. alert(getPageSize[0] + getPageSize[1] + getPageSize[2] + getPageSize[3]);

银子曰:有事早奏!无事退朝!

发表评论

验证码: 验证码看不清发泄点这里

 

浏览最多的10篇日志

评论最多的10篇日志

随机显示的10篇日志