图解offsetWidth, clientWidth, scrollWidth, innerWidth, outerWidth, pageXOffset等

本文深入探讨了不同浏览器环境下,DOM对象如div的offsetWidth、clientWidth、scrollWidth等尺寸属性的具体表现,以及window对象的outerWidth、innerWidth、pageXOffset和screenLeft等属性在各种情况下的行为差异。

不同浏览器:
在这里插入图片描述

**一、测试1:无滚动条时,dom对象的offsetWidth、clientWidth和scrollWidth

(1)测试代码**

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>Javascript</title>
<style>
*{margin: 0;padding: 0;}
body{font: 12px/2 Arial;background-color: #ccc;padding: 20px;}
#b1{width: 530px;height: 320px;background-color: #fff;position: relative;}
#b2{width: 220px;height: 130px;background-color: orange;border: 10px solid #085A90;padding: 10px;position: relative;left: 140px;top: 90px;}
</style>
</head>
<body>
<div id="b1"><div id="b2"></div></div>
<script>
window.onload = function(){	
	var oB2 = document.getElementById('b2');
 
	console.log("offsetWidth="+oB2.offsetWidth, "offsetHeight="+oB2.offsetHeight);
	console.log("clientWidth="+oB2.clientWidth, "clientHeight="+oB2.clientHeight);
	console.log("offsetLeft="+oB2.offsetLeft, "offsetTop="+oB2.offsetTop);
	console.log("scrollWidth="+oB2.scrollWidth, "scrollHeight="+oB2.scrollHeight);
	console.log("scrollLeft="+oB2.scrollLeft, "scrollTop="+oB2.scrollTop);
}
</script>
</body>
</html>

(2)测试结果
在这里插入图片描述
IE7下,scrollWidth = 20,scrollHeight = 34

(3)图解结果
在这里插入图片描述

二、测试2:有滚动条时,dom对象offsetWidth、clientWidth 和 scrollWidth

(1)测试代码

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title>Javascript</title>
<style>
*{margin: 0;padding: 0;}
body{font: 12px/2 Arial;background-color: #ccc;padding: 20px;}
#b1{width: 300px;height: 220px;background-color: #fff;position: relative;overflow: auto;border: 10px solid #999;padding: 10px;}
#b2{width: 220px;height: 330px;background-color: orange;border: 10px solid #085A90;padding: 10px;position: relative;left: 140px;top: 90px;}
</style>
</head>
<body>
<div id="b1"><div id="b2"></div></div>
<script>
window.onload = function(){
	var oB1 = document.getElementById('b1');
	console.log("offsetWidth="+oB1.offsetWidth, "offsetHeight="+oB1.offsetHeight);
	console.log("clientWidth="+oB1.clientWidth, "clientHeight="+oB1.clientHeight);
	console.log("offsetLeft="+oB1.offsetLeft, "offsetTop="+oB1.offsetTop);
	console.log("scrollWidth="+oB1.scrollWidth, "scrollHeight="+oB1.scrollHeight);
	console.log("scrollLeft="+oB1.scrollLeft, "scrollTop="+oB1.scrollTop);
}
</script>
</body>
</html>

(2)测试结果

这里我们需要读取外层带滚动条的 div#b1 的值。ie7~ie11、chrome 和 firefox 结果一致,如下:
在这里插入图片描述
(3)图解结果
在这里插入图片描述

三、测试3:window对象的 outerWidth、innerWidth、pageXOffset 和 screenLeft(screenX)

(1)测试代码

<script>
window.onload = function(){
	console.log("innerWidth="+window.innerWidth, "innerHeight=" + window.innerHeight);
	console.log("outerWidth="+window.outerWidth, "outerHeight=" + window.outerHeight);
	console.log("pageXOffset="+window.pageXOffset, "pageYOffset=" + window.pageYOffset);
	console.log("screenX="+window.screenX, "screenY=" + window.screenY);
	console.log("screenLeft="+window.screenLeft, "screenTop=" + window.screenTop);
}
</script>

(2)图解结果(不同浏览器下,console 输出与下图部分有不同)
在这里插入图片描述
screenX 和 screenLeft 非常混乱,详细参考:http://blog.youkuaiyun.com/u010200222/article/details/10948059

这三个属性 `scrollWidth`、`offsetWidth` `clientWidth` 都是用于获取 DOM 元素的宽度,但它们的用途计算方式有所不同。下面将详细解释它们的区别,并给出代码示例帮助你理解。 --- ### ✅ `clientWidth` - **定义**:元素内部的可视区域宽度(不包括边框、滚动条、外边距)。 - **组成**:`width + padding`(不包括 border margin)。 - **是否包含滚动条**:不包含。 ```javascript const element = document.getElementById('myDiv'); console.log(element.clientWidth); // clientWidth ``` --- ### ✅ `offsetWidth` - **定义**:元素的可见区域总宽度,包括内容、内边距、边框。 - **组成**:`width + padding + border`。 - **是否包含滚动条**:包含滚动条(如果存在)。 - **注意**:这个值是只读的。 ```javascript console.log(element.offsetWidth); // offsetWidth ``` --- ### ✅ `scrollWidth` - **定义**:元素内容的实际宽度,包括不可见的部分(即被隐藏的部分)。 - **组成**:内容的实际宽度(即使内容被滚动条隐藏了,也计算在内)。 - **是否包含滚动条**:不包含。 ```javascript console.log(element.scrollWidth); // scrollWidth ``` --- ### ✅ 总结对比表格 | 属性名 | 包含内容 | 是否包括滚动条 | 是否包括边框 | 是否包括隐藏内容 | |---------------|------------------|----------------|----------------|------------------| | `clientWidth` | width + padding | ❌ | ❌ | ❌ | | `offsetWidth` | width + padding + border | ✅ | ✅ | ❌ | | `scrollWidth` | 实际内容宽度 | ❌ | ❌ | ✅ | --- ### ✅ 使用场景举例 - **判断文字是否被截断(省略号显示)**: ```javascript function isTruncated(element) { return element.scrollWidth > element.offsetWidth; } ``` - **获取元素的可见区域大小(包括边框)**: ```javascript const visibleWidth = element.offsetWidth; ``` - **获取元素内部完整内容宽度(即使被隐藏)**: ```javascript const fullContentWidth = element.scrollWidth; ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值