js 中offsetTop、offsetLeft、offsetWidth、offsetHeight详解
来源:互联网 发布:全知视角和有限视角 编辑:程序博客网 时间:2024/06/03 06:43
1. 偏移量共包括offsetTop、offsetLeft、offsetWidth、offsetHeight
元素:内容大小(width、height)、内边距(padding)、边框(border)、外边距(margin)、滚动条(scroll)
【1】offsetWidth:元素在水平方向上占据的大小,无单位
offsetWidth = border + 元素内容宽度 + padding
= border-left-width + padding-left- width + width + padding-right-width + border-right-width
【2】offsetHeight:元素在垂直方向上占据的大小,无单位
offsetHeight = border + 元素内容高度 + padding
= border-left-height+ padding-left- height+ height+ padding-height-width + border-right-height
注:1. 如果存在垂直滚动条,offsetWidth也包括垂直滚动条的宽度;
2. 如果存在水平滚动条,offsetHeight也包括水平滚动条的高度
<div id="test" style="width:100px; height:100px; padding:10px; margin:10px; border:1px solid black;"></div> <script>//122=1+10+100+10+1console.log(test.offsetWidth);console.log(test.offsetHeight);</script>有滚动条的情况
<div id="test" style="width:100px; height:100px; padding:10px; margin:10px; border:1px solid black; overflow: scroll;"></div> <script>//IE8-浏览器将垂直滚动条的宽度计算在width宽度和height高度中,width和height的值仍然是100px;//而其他浏览器则把垂直滚动条的宽度从width宽度中移出,把水平滚动条的高度从height高度中移出,则滚动条宽度为17px,width宽度和height高度为剩下的83pxif(window.getComputedStyle){ console.log(getComputedStyle(test).width,getComputedStyle(test).height)//83px}else{ console.log(test.currentStyle.width,test.currentStyle.height);//100px}//122=1+10+100+10+1console.log(test.offsetWidth,test.offsetHeight);</script>
【3】offsetTop: 表示元素的上外边框至offsetParent元素的上内边框之间的像素距离
【4】offsetLeft:表示元素的左外边框至offsetParent元素的左内边框之间的像素距离
<div id="out" style="padding: 5px;position: relative;background-color: pink;margin: 6px;border:1px solid black"> <div id="test" style="width:100px; height:100px; margin:10px;background-color:green;"></div> </div><script>//15=test.marginTop(10) + out.paddingTop(5)alert(test.offsetTop);//15=test.marginLeft(10) + out.paddingLeft(5)alert(test.offsetLeft);</script>
注:
【1】所有偏移量属性都是只读的
<div id="test" style="width:100px; height:100px; margin:10px;"></div> <script>console.log(test.offsetWidth);//100//IE8-浏览器会报错,其他浏览器则静默失败test.offsetWidth = 10;console.log(test.offsetWidth);//100</script>
【2】如果给元素设置了display:none,则它的偏移量属性都为0
<div id="test" style="width:100px; height:100px; margin:10px;display:none"></div><script>console.log(test.offsetWidth);//0console.log(test.offsetTop);//0</script>
【3】每次访问偏移量属性都需要重新计算
<div id="test" style="width:100px; height:100px; margin:10px;"></div> <script>console.time("time");var a = test.offsetWidth;for(var i = 0; i < 100000; i++){ var b = a;}console.timeEnd('time');//1.428ms</script>
文章转载至:http://www.cnblogs.com/xiaohuochai/p/5828369.html
阅读全文
0 0
- js 中offsetTop、offsetLeft、offsetWidth、offsetHeight详解
- offsetTop、offsetLeft、offsetWidth、offsetHeight
- offsetTop、offsetLeft、offsetWidth、offsetHeight
- offsetLeft、offsetTop、offsetWidth,offsetHeight
- offsetTop、offsetLeft、offsetWidth、offsetHeight
- offsetTop、offsetLeft、offsetWidth、offsetHeight(转帖)
- offsetTop、offsetLeft 、offsetWidth、offsetHeight 算法
- 关于offsetWidth, offsetHeight,offsetTop, offsetLeft
- offsetWidth、offsetHeight、offsetLeft、offsetTop、offsetParent
- offsetTop、offsetLeft、offsetWidth、offsetHeight、style中的样式
- offsetTop、 offsetLeft、offsetWidth、offsetHeight的用法
- offsetTop、offsetLeft、offsetWidth、offsetHeight、offsetParent、offsetrect
- offsetLeft,offsetTop,offsetWidth,offsetHeight的使用
- offsetLeft offsetHeight offsetTop offsetWidth offsetParent学习笔记
- offsetTop,offsetLeft,offsetWidth,offsetHeight,scrollLeft,scrollWidth,clientWidth详细
- offsetWidth offsetHeight offsetTop offsetLeft style.top style.left
- offsetTop.offsetLeft,offsetWidth,offsetHeight,offsetParent,scrollTop,scrollLeft,scrollWidth,scrollHeight区别
- offsetLeft、offsetTop、offsetWidth、offsetHeight……的属性
- C语言inline函数
- HTML中关于justify(text-align)属性无效果解决讲解
- 整数数组中找出只出现了一次的数
- angularjs中方法执行的一个小tip
- 理解null值和可空类型
- js 中offsetTop、offsetLeft、offsetWidth、offsetHeight详解
- caffe深度学习网络(.prototxt)在线可视化工具:Netscope Editor
- 设计模式--工厂模式
- 多线程-多线程方式3的思路及代码实现:方式3依赖于线程池存在的
- ubuntu16.04下apache部署两个CI网站
- 构造方法的作用
- 人工智能全球化,中国科技企业正在加速布局/智库2861
- 数据模型访问结构
- 100个句子直刷7000单词