读书人

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

发布时间: 2012-11-03 10:57:44 作者: rapoo

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

作者 : 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());

读书人网 >Web前端

热点推荐