计算浏览器窗口和页面的高度和宽度

作者 : zhanhailiang    日期 : 2012-10-30

计算浏览器窗口的高度和宽度:

    var windowSize = function (a) {
        var b, c, d;
        a ? d = a.document : d = document;
        if (d.compatMode === "CSS1Compat") {
            b = d.documentElement.clientWidth;
            c = d.documentElement.clientHeight
        } else if (self.innerHeight) {
            a ? d = a.self : d = self;
            b = d.innerWidth;
            c = d.innerHeight
        } else if (d.documentElement && d.documentElement.clientHeight) {
            b = d.documentElement.clientWidth;
            c = d.documentElement.clientHeight
        } else if (d.body) {
            b = d.body.clientWidth;
            c = d.body.clientHeight
        }
        return {
            width: b,
            height: c
        }
    };

    console.log(windowSize());

计算页面的高度和宽度:

    var pageSize = function(b) {
        b ? target = b.document : target = document;
        var c = target.compatMode == "CSS1Compat" ? target.documentElement : target.body,
            d, e, f, g;
        if (window.innerHeight && window.scrollMaxY) {
            d = c.scrollWidth;
            e = window.innerHeight + window.scrollMaxY
        } else if (c.scrollHeight > c.offsetHeight) {
            d = c.scrollWidth;
            e = c.scrollHeight
        } else {
            d = c.offsetWidth;
            e = c.offsetHeight
        }
        var h = windowSize();
        e < h.height ? f = h.height : f = e;
        d < h.width ? g = h.width : g = d;
        return {
            page: {
                width: g,
                height: f
            }, win: {
                width: h.width,
                height: h.height
            }
        }
    };

    console.log(pageSize());

你可能感兴趣的:(浏览器)