javascript中offsetWidth,clientWidth,innerWidth相关

元素属性和元素方法都通过 elem.属性 或 elem.方法 的方式使用,window属性通过 window.属性 的方式使用,document属性则通过document调用。

<script>
     1,元素视图属性
     offsetWidth 水平方向 width + 左右padding + 左右border-width
     offsetHeight 垂直方向 height + 上下padding + 上下border-width
      
     clientWidth 水平方向 width + 左右padding - 滚动条width(盒子有滚动条时)
     clientHeight 垂直方向 height + 上下padding
     
     offsetTop 获取当前元素到 定位父节点 的top方向的距离
     offsetLeft 获取当前元素到 定位父节点 的left方向的距离
     
     scrollWidth 元素内容真实的宽度,内容不超出盒子高度时为盒子的clientWidth
     scrollHeight 元素内容真实的高度,内容不超出盒子高度时为盒子的clientHeight
     
     2,Window视图属性(低版本IE浏览器[<IE9]不支持,亲测包含滚动条)
     innerWidth 浏览器窗口宽度
     innerHeight 浏览器窗口高度
     
     3,Document文档视图
     *(低版本IE的innerWidth、innerHeight的代替方案)
     document.documentElement.clientWidth || document.body.clientWidth 网页可视区宽度(不包括滚动条,边框)
     document.documentElement.clientHeight || document.body.clientHeight 网页可视区高度(同上)

     document.documentElement.offsetWidth || document.body.offsetWidth 网页可视区宽度(不包括滚动条)
     document.documentElement.offsetHeight 获取整个文档的高度(包含body的margin)
     document.body.offsetHeight 获取整个文档的高度(不包含body的margin)

     document.body.scrollWidth 网页正文全文宽 
     document.body.scrollHeight 网页正文全文高

     document.documentElement.scrollTop 返回文档的滚动top方向的距离(当窗口发生滚动时值改变)
     document.documentElement.scrollLeft 返回文档的滚动left方向的距离(当窗口发生滚动时值改变)
      
     4,元素方法
     (1) getBoundingClientRect() 获取元素到body
       bottom: 元素底边(包括border)到可视区最顶部的距离
       left: 元素最左边(不包括border)到可视区最左边的距离
       right: 元素最右边(包括border)到可视区最左边的距离
       top: 元素顶边(不包括border)到可视区最顶部的距离
       height: 元素的offsetHeight
       width: 元素的offsetWidth
       x: 元素左上角的x坐标 
       y: 元素左上角的y坐标
     (2) scrollIntoView() 让元素滚动到可视区
     
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值