网络编程
位置:首页>> 网络编程>> JavaScript>> JS获取页面窗口实际大小函数

JS获取页面窗口实际大小函数

作者:银子 来源:银子的blog 发布时间:2008-01-28 13:18:00 

标签:窗口,页面,宽度,函数,js

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

什么是Lightbox?下载lightbox源代码? -->Lightbox JS v2.0

function getPageSize(){
   
    var xScroll, yScroll;
   
    if (window.innerHeight && window.scrollMaxY) {   
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
   
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }   
   
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }
 
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){   
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
 
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}

调用方法:

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

getPageSize[0]保存的是页面宽度,getPageSize[1]保存的是页面高度,getPageSize[2]保存的是窗口宽度,getPageSize[3]保存的是窗口高度。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com