你真的懂js获取可视区宽高吗
栏目: JavaScript · 发布时间: 8年前
内容简介:原生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);
复制代码
- ie8下各个值
- ie9下各个值
- ie10跟ie9一样不列图了
- ie11下各个值
- chorme浏览器下各个值
通过以上各图对比不难看出(先排除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获取可视区宽高吗》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
分布式服务架构:原理、设计与实战
李艳鹏、杨彪 / 电子工业出版社 / 2017-8 / 89.00
《分布式服务架构:原理、设计与实战》全面介绍了分布式服务架构的原理与设计,并结合作者在实施微服务架构过程中的实践经验,总结了保障线上服务健康、可靠的最佳方案,是一本架构级、实战型的重量级著作。 《分布式服务架构:原理、设计与实战》以分布式服务架构的设计与实现为主线,由浅入深地介绍了分布式服务架构的方方面面,主要包括理论和实践两部分。理论上,首先介绍了服务架构的背景,以及从服务化架构到微服务架......一起来看看 《分布式服务架构:原理、设计与实战》 这本书的介绍吧!