代码之家  ›  专栏  ›  技术社区  ›  Shamik

启动期间javascript窗口大小中断

  •  0
  • Shamik  · 技术社区  · 14 年前

    我使用下面的代码来获得窗口的宽度和高度

    function pageWidth() {
        return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
    }
    function pageHeight() {
        return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
    }
    

    我在页面加载期间调用这个javascript。如果我保持我的URL作为主页,并尝试打开资源管理器,我看到的大部分时间,这些函数给我的值等于0。

    有没有更好的方法来测量窗户的宽度?

    2 回复  |  直到 14 年前
        1
  •  0
  •   Luka Milani    14 年前

    如果可以帮助我使用这个函数,这是更交叉的,你可以调用它在事件加载为 迈克尔 建议

    // getPageSize()
    // Returns array with page width, height and window width, height
    // Core code from - quirksmode.org
    //
    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;
    }
    
        2
  •  0
  •   donohoe    14 年前

    如果问题是页面仍在加载,那么您应该等到加载完成后再运行代码:

    window.addEventListener('load', function(){
      // ok, now good to call those functions and get values back!
    }, false);