JavaScript属性中的offsetLeft、offsetWidth、clientWidth、scrollLeft、scrollWidth、innerWidth
1.offsetLeft和offsetTop
只读属性,返回当前元素与父辈元素之间的距离(不包括边框)。其中父辈元素的取法是有门道的:
(1).若父辈元素中有定位的元素,那么就返回距离当前元素最近的定位元素的距离。
(2).若父辈元素中没有定位元素,那么就返回相对于body的距离。
(3).若当前元素具有固定定位(position:fixed;),那么返回的是当前元素与可视窗口的距离。
<div id="a" style="width:400px;height:400px;margin:100px auto 0;background-color: red;">
<div id="b" style="position:relative;width:200px;height:200px;margin:100px auto 0;background-color: blue;">
<div id="c" style="position: fixed;width:50px;height:50px;top:200px;left: 200px;background-color: green;"></div>
<div id="d" style="position: absolute;top:50px;left: 50px;height:100px;width:100px;background-color: yellow">
<div id="e" style="width:50px;height:50px;margin:25px auto 0;background-color: darkred;"></div>
</div>
</div>
</div>
<script>
var a=document.getElementById("a");
var b=document.getElementById("b");
var c=document.getElementById("c");
var d=document.getElementById("d");
var e=document.getElementById("e");
console.log("e:"+ e.offsetLeft, e.offsetTop);// e: 25,25。以具有绝对定位的父辈元素d为参照
console.log("d:"+ d.offsetLeft, d.offsetTop);// d: 50,50。以其相对定位的父元素b为参照
console.log("c:"+ c.offsetLeft, c.offsetTop);// c: 200,200。以可视窗口为参照
console.log("b:"+ b.offsetLeft, b.offsetTop);// b: 612,100。以body为参照
console.log("a:"+ a.offsetLeft, a.offsetTop);// a: 512,100。以body为参照
</script>
2.offsetHeight、offsetWidth和style.height、style.width区别
offsetHeight、offsetWidth返回的是
数值;style.height、style.width返回的是字符串,单位是“px”offsetHeight、offsetWidth
只读;style.height、style.width可读写offsetHeight、offsetWidth包括元素的
边框、内边距;offsetWidth=leftborder+leftpadding+width+rightpadding+rightborder;style.height、style.width只包括元素height、width
如果没有为元素设置高度,offsetHeight会
根据内容获取高度值,style.height会返回undefindjquery中使用
$(obj).height()、$(obj).css('height')来获取元素的高度,返回的是一个带有单位的字符串<div id="a" style="width:50px;height:50px;border:5px solid red;padding:10px;margin:50px;background: black;"></div>
<script>
var a=document.getElementById("a");
console.log(a.offsetHeight, a.offsetWidth);//80,80
console.log(a.style.height, a.style.width);//50px,50px
</script>
3.clientWidth和clientHeight
只读属性,返回当前节点的可视宽度和可视高度(不包括边框、外边距)(包括内边距)clientHeight = topPadding + bottomPadding+ height - scrollbar.height。
<div id="a" style="width:100px;height:50px;border:25px solid blue;background-color:red;overflow: auto;">
hello world!<br> hello world!<br> hello world!<br> hello world!<br> hello world!<br> hello world!<br> hello world!<br> hello world!<br> hello world!<br>
</div>
<script>
var a=document.getElementById("a");
console.log("a:"+ a.clientWidth, a.clientHeight);// a: 83,50。不包括边框和滚动条(17px)
</script>
4.scrollWidth和scrolltHeight
只读属性,返回当前节点的实际宽度和实际高度(不包括边框),没有滚动条时和clientWidth和clientHeight一样
上例中:
console.log("a:"+ a.scrollWidth, a.scrollHeight);// a: 83,324。不包括边框和滚动条的宽度,返回实际高度和宽度
5.scrollTop和scrollLeft
可读写属性
scrollTop:返回网页滚动条垂直方向滚去的距离;
scrollLeft:返回网页滚动条水平方向滚去的距离;
6.innerHeight和innerWidth
只读属性,返回窗口文档显示区的高度和宽度,不包括菜单栏、工具栏和滚动条的宽高。
IE不支持这些属性。它用 document.documentElement 或 document.body (与 IE 的版本相关)的 clientWidth 和 clientHeight 属性作为替代。
7.outerrHeight和outerWidth
outerHeight属性设置或返回一个窗口的高度,包括所有界面元素(如工具栏/滚动条)。
outerWidth属性设置或返回窗口的宽度,包括所有的界面元素(如工具栏/滚动)。
JavaScript属性中的offsetLeft、offsetWidth、clientWidth、scrollLeft、scrollWidth、innerWidth的更多相关文章
- JavaScript问题——在浏览器中的offsetLeft/offsetWidth等属性是什么?
原文链接http://www.cnblogs.com/xiaohuochai/p/5828369.html https://blog.csdn.net/u012532033/article/detai ...
- style.left offsetLeft offsetwidth clientLeft clientWidth scrollLeft scrollWidth
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 一文看懂js中元素的滚动大小(scrollWidth,scrollHeight,scrollTop,scrollLeft)
滚动大小(scroll dimension) 滚动大小指的是包含滚动内容元素的大小. 以下是与元素滚动内容大小相关的属性: 1. scrollWidth:在没有滚动条的情况下,元素内容的总宽度. 2. ...
- scrollWidth,offsetWidth,clientWidth,width;scrollHeight,offsetHeight,clientHeight,height;offsetTop,scrollTop,top;offsetLeft,scrollLeft,left还有谁
题中的那么多属性让人头都大了,他们到底是什么意思?不同浏览器的实现是一样的吗?以下所有结论来自chrome版本 53.0.2785.89 (64-bit)和firefox版本52.0.2,操作系统ub ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 scrollHeight: 获取对象的滚动高度. scrollLe ...
- 完全图解scrollLeft,scrollWidth,clientWidth,offsetWidth 获取相对途径,滚动图片(网上找的,未经试验,但觉得比较好)
获取元素的位置属性可以通过 HTMLElement.offsetLeft HTMLElement.offsetTop 但是,这两个属性所储存的数值并不是该元素相对整个浏览器画布的绝对位置,而是相对于其 ...
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之全然具体解释
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth究竟指的哪到哪的距离之全然具体解释scrollHeight: 获取对象的滚动高度. scrol ...
- H5,PC网页屏幕尺寸相关整理(scrollLeft,scrollWidth,clientWidth,offsetWidth)
HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解scrollHeight: 获取对象的滚动高度. scrollLef ...
随机推荐
- 快速傅里叶变换FFT
多项式乘法 #include <cstdio> #include <cmath> #include <algorithm> #include <cstdlib ...
- C++重载运算符的规则
(1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载. 例如,有人觉得BASIC中用“* *”作为幂运算符很方便,也想在C++中将“* *”定义为幂运算符,用“3* *5”表示3 ...
- [poj 3678]Katu Pazzle[2-SAT常用建图法]
题意: 不说了..典型的2-SAT 常用模型: 重点: 突出"绑定性". 连线表示限制而非可行. 因为最后要求对立点不在同一强连通分量是说同一强连通中的点必须同时选. 坑: 首先是 ...
- 1.Solution的Build、Rebuild和Clean
大家好,我是原文,这篇随笔是对原文的翻译以及自己的体会. 做程序员没追求的话是永远找不到女朋友的,当然有追求也找不到,这个先不提,好在有追求的时候我是充实而且开心的.现在我们的问题是,每天调试项目,在 ...
- 内容高度小于窗口高度时版权div固定在底部
<!doctype html><html><head><meta charset="utf-8"><title>文档内容 ...
- 虚拟键盘,移动web开发的痛
原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方.如下图: 如果移动端web也想做类似的功能,可以实现吗? 你可能会说着:“不就是放一个position: fixed;的输入框在 ...
- Javascript 自定义事件 (custom event)
Javascript 中经常会用到自定义事件.如何创建一个简单的自定义事件呢?在创建自定义的事件之前,我们应该考虑一下和事件有关的东西.例如 click 事件,首先我们要能注册一个click事件(在一 ...
- .NET软件开发与常用工具清单
[工欲善其事,必先利其器]软件开发的第一步就是选择高效.智能的工具. 下面列出的工具软件能辅助提高工作效率. 开发类工具 微软.Net平台下的集成开发环境:Visual Studio. Visual ...
- css区分ie6,7,ff
IE6能识别*,但不能识别 !important,IE7能识别*,也能识别!important;FF不能识别*,但能识别!important; 可以这样区别FF,IE7,IE6: background ...
- 关于Google指令(别提baidu)
关于google指令 关于google指令 google为我们准备好了的"指令"(directive),可以最大限度帮助我们完成每一次搜索.这些指令其实就是一个个关键字,能让我们从 ...