【问题标题】:Get viewport height excluding horizontal scrollbar?获取不包括水平滚动条的视口高度?
【发布时间】:2013-11-12 03:48:51
【问题描述】:

window.innerHeight 给出视口的高度包括水平滚动条的高度。有没有办法找到 usable 内部高度,即 包含水平滚动条的方法?

理想情况下,我正在寻找一种纯 JS 解决方案,但如果没有,jQuery 等也可以。

【问题讨论】:

标签: javascript jquery height viewport


【解决方案1】:

我在这里找到了解决方案:scrollbar detection demo (我可以在这里放置其他人网站的链接吗?)

用到了以下两个函数:

// check current presence of H & V scrollbars
// @return array [ Boolean, Boolean ]
function getSBLive(w) {
    var d = w.document, c = d.compatMode;
    r = c && /CSS/.test(c) ? d.documentElement : d.body;
    if (typeof w.innerWidth == 'number') {
        return [ w.innerWidth > r.clientWidth, w.innerHeight > r.clientHeight ];
    } else {
        return [ r.scrollWidth > r.clientWidth, r.scrollHeight > r.clientHeight ];
    }
}

// get current H & V scrollbars tickness
// @return array [ Number, Number ]
function getSBSize(w) {
    var d = w.document, b = d.body, r = [ 0, 0 ], t;
    if (b) {
        t = d.createElement('div');
        t.style.cssText = 'position:absolute;overflow:scroll;top:-100px;left:-100px;width:100px;height:100px;';
        b.insertBefore(t, b.firstChild);
        r = [ t.offsetHeight - t.clientHeight, t.offsetWidth - t.clientWidth ];
        b.removeChild(t);
    }
    return r;
}

然后您可以使用这些函数来查找没有滚动条的窗口高度:

var sbLive = getSBLive(window);
var sbSize = getSBSize(window);

var windowHeightWithoutScrollBar = sbLive[0] ? sbSize[0] : 0;

【讨论】:

    【解决方案2】:

    如果我告诉你这比你想象的要容易(或者比你当时想象的要容易)document.body.clientWidth;

    【讨论】:

      【解决方案3】:

      jQuery 解决方案是$(window).height();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-04
        • 2010-10-02
        • 1970-01-01
        • 2013-11-12
        • 1970-01-01
        • 2015-06-21
        相关资源
        最近更新 更多