关于offsetWidth的基本用法可以参阅js offsetWidth一章节。
下面就通过代码实例验证一下offsetWidth是否包括滚动条的宽度。
代码实例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>前端部落</title>
<style>
#top,#bottom{
width:200px;
height:100px;
background:red;
overflow:auto;
margin:50px;
}
#top_child,#bottom_child {
width:20px;
background:blue;
}
#top_child{
height:160px;
}
#bottom_child{
height:60px;
}
</style>
<script>
window.onload = function () {antzone
var oantzone = document.getElementById("antzone");
var otop = document.getElementById("top");
var obottom = document.getElementById("bottom");
var str = "";
str = str + "top元素的offsetWidth:" + otop.offsetWidth+"<br/>";
str = str + "bottom元素的offsetWidth:" + obottom.offsetWidth;
oantzone.innerHTML = str;
}
</script>
</head>
<body>
<div id="top">
<div id="top_child"></div>
</div>
<div id="bottom">
<div id="bottom_child"></div>
</div>
<div id="antzone"></div>
</body>
</html>
由上面的代码可以证明,offsetWidth是包括滚动条的尺寸的。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。