你真的懂js获取可视区宽高吗

栏目: JavaScript · 发布时间: 7年前

内容简介:原生js获取高度不就是就window.innerHeight一句话的事,可是真的这么简单吗顺便你也可以看看document.body和document.documentElement在各个浏览器的差异;document.documentElement返回的是整个文档的根节点即 html标签;document.body 返回的是DOM对象里的body子节点,即 body 标签window.innerHeight = document.documentElement.clientHeight + 滚动条高度;

原生js获取高度不就是就window.innerHeight一句话的事,可是真的这么简单吗

来看个测试页面,如果页面带有横向纵向的滚动条,我们打印出各个高度进行查看对比

顺便你也可以看看document.body和document.documentElement在各个浏览器的差异;document.documentElement返回的是整个文档的根节点即 html标签;document.body 返回的是DOM对象里的body子节点,即 body 标签

console.log('document.documentElement.clientHeight-' + document.documentElement.clientHeight);
console.log('document.documentElement.scrollHeight-' + document.documentElement.scrollHeight);
console.log('document.documentElement.offsetHeight-' + document.documentElement.offsetHeight);
console.log('document.body.clientHeight-' + document.body.clientHeight);
console.log('document.body.scrollHeight-' + document.body.scrollHeight);
console.log('document.body.offsetHeight-' + document.body.offsetHeight);
console.log('window.innerHeight-' + window.innerHeight);
复制代码
  1. ie8下各个值
你真的懂js获取可视区宽高吗
  1. ie9下各个值
你真的懂js获取可视区宽高吗
  1. ie10跟ie9一样不列图了
  2. ie11下各个值
你真的懂js获取可视区宽高吗
6. 火狐浏览器下各个值
你真的懂js获取可视区宽高吗
  1. chorme浏览器下各个值
你真的懂js获取可视区宽高吗

通过以上各图对比不难看出(先排除ie8)

window.innerHeight = document.documentElement.clientHeight + 滚动条高度;

如果没有滚动条则window.innerHeight = document.documentElement.clientHeight

在来说说ie8

ie8比较特殊不支持window.innerHeight并且html还自带有2像素的边框; 可以通过document.documentElement.offsetHeight - 2 * 2得到window.innerHeight的值

所以ie8的window.innerHeight = document.documentElement.offsetHeight - 2 * 2 = document.documentElement.clientHeight + 滚动条高度。

如果没有滚动条window.innerHeight = document.documentElement.offsetHeight - 2 * 2 = document.documentElement.clientHeight

所以获取可视区的高度不是简单的window.innerHeight,真正的可视区高度不应该包括滚动条

/**
*  获取视口宽高 兼容兼容到ie8
*  @param {boolean} flag 标识返回的宽高是否包含滚动条
*  @return {object} {widht: xxx, height: xxx} 视口宽高
/ 
function getViewPort (flag) {
    if (typeof flag === 'undefined') {
        return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight
        };
    }
    if (flag === true) {
        // ie8 html 有2像素边框 上下, 左右 4像素
        return {
            width: window.innerWidth || document.documentElement.offsetWidth - 2 * 2,
            height: window.innerHeight || document.documentElement.offsetHeight - 2 * 2
        };
    }
}
复制代码

获取文档的宽高呢

通过以上各图的对比,整个文档的高度,可以通过document.documentElement.scrollHeight来获取各个浏览器都比较一致,你也不必纠结到底是用document.body 还是用document.documentElement; 用clientHeight还是offsetHeight

/**
*  获取文档宽高 兼容兼容到ie8
* 
*  @return {object} {widht: xxx, height: xxx} 视口宽高
/ 
function getDocumentPort (flag) {
    return {
        width: document.documentElement.scrollWidth,
        height: document.documentElement.scrollHeight
    };
}

复制代码

以上所述就是小编给大家介绍的《你真的懂js获取可视区宽高吗》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

High Performance Python

High Performance Python

Andrew Lewis / O'Reilly Media, Inc. / 2010-09-15 / USD 34.99

Chapter 1. Introduction Section 1.1. The High Performance Buzz-word Chapter 2. The Theory of Computation Section 2.1. Introduction Section 2.2. Problems Section 2.3. Models of Computati......一起来看看 《High Performance Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试