zl程序教程

您现在的位置是:首页 >  其他

当前栏目

offsetWidth是否包括滚动条

2023-04-18 16:48:53 时间

关于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是包括滚动条的尺寸的。