珠峰培训

JavaScript如何获取浏览器的高度和宽度以及浏览器滚动条的位置?

作者:

2014-02-28 16:42:38

482

JavaScript获取浏览器的高度和宽度,程序代码如下所示

function getBrowser() {
    var w, h;
    w = document.documentElement.clientWidth || document.body.clientWidth;
    h = document.documentElement.clientHeight || document.body.clientHeight;
    return {
        width: w,
        height: h
    };
}

JavaScript获取浏览器滚动条的位置,程序代码如下所示

function scollPostion() {
    var t, l, w, h;
    if (document.documentElement && document.documentElement.scrollTop) {
        t = document.documentElement.scrollTop;
        l = document.documentElement.scrollLeft;
        w = document.documentElement.scrollWidth;
        h = document.documentElement.scrollHeight;
    } else if (document.body) {
        t = document.body.scrollTop;
        l = document.body.scrollLeft;
        w = document.body.scrollWidth;
        h = document.body.scrollHeight;
    }
    return {
        top: t,
        left: l,
        width: w,
        height: h
    };
}