前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >offsetWidth是否包括滚动条

offsetWidth是否包括滚动条

原创
作者头像
IT工作者
发布2021-12-29 10:54:48
3480
发布2021-12-29 10:54:48
举报
文章被收录于专栏:程序技术知识程序技术知识

关于offsetWidth的基本用法可以参阅js offsetWidth一章节。

下面就通过代码实例验证一下offsetWidth是否包括滚动条的宽度。

代码实例如下:

代码语言:javascript
复制
<!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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档