转摘
IE6.0、FF1.06+:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width - border
clientHeight = height - border
offsetWidth = width
offsetHeight = height
(需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)
具体效果请参见下面的代码:
js 代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
- <html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]" lang="gb2312">
- <head>
- <head>
- <title> 代码实例:关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较 </title>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <meta name="author" content="枫岩,CnLei.y.l@gmail.com">
- <meta name="copyright" content="[url=http://www.cnlei.com]http://www.cnlei.com[/url]" />
- <meta name="description" content="关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较" />
- <style type="text/css" media="all">
- body {font-size:14px;}
- a,a:visited {color:#00f;}
- #Div_CnLei {
- width:300px;
- height:200px;
- padding:10px;
- border:10px solid #ccc;
- background:#eee;
- font-size:12px;
- }
- #Div_CnLei p {margin:0;padding:10px;background:#fff;}
- </style>
- <script type="text/javascript">
- function Obj(s){
- return document.getElementById(s)?document.getElementById(s):s;
- }
- function GetClientWidth(o){
- return Obj(o).clientWidth;
- }
- function GetClientHeight(o){
- return Obj(o).clientHeight;
- }
- function GetOffsetWidth(o){
- return Obj(o).offsetWidth;
- }
- function GetOffsetHeight(o){
- return Obj(o).offsetHeight;
- }
- </script>
- </head>
- <body>
- <p>点击下面的链接:</p>
- <div id="Div_CnLei">
- <p><a href="javascript:alert(GetClientWidth('Div_CnLei'));">GetClientWidth();</a> <a href="javascript:alert(GetClientHeight('Div_CnLei'));">GetClientHeight();</a></p>
- <p><a href="javascript:alert(GetOffsetWidth('Div_CnLei'));">GetOffsetWidth();</a> <a href="javascript:alert(GetOffsetHeight('Div_CnLei'));">GetOffsetHeight();</a></p>
- </div>
- <div id="Description">
- <p><strong>IE6.0、FF1.06+:</strong><br />
- clientWidth = width + padding = 300+10×2 = 320<br />
- clientHeight = height + padding = 200+10×2 = 220<br />
- offsetWidth = width + padding + border = 300+10×2+10×2= 340<br />
- offsetHeight = height + padding + border = 200+10×2+10×2 = 240</p>
- <p><strong>IE5.0/5.5:</strong><br />
- clientWidth = width - border = 300-10×2 = 280<br />
- clientHeight = height - border = 200-10×2 = 180<br />
- offsetWidth = width = 300<br />
- offsetHeight = height = 200</p>
- </div>
- </body>
- </html>
分享到:
相关推荐
四种浏览器对_clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth_和_scrollWidth_的解
scrollLeft,scrollWidth,clientWidth,offsetWidth完全详解 2009年12月14日,16:58:19 | ArthurXF ...(需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)
前段时间在LJW写的touchslider.js轮播代码里添加自适应屏幕大小的功能时,遇到一个问题。...obj.offsetWidth(offsetHeight); obj.clientWidth(clientHeight); getComputedStyle 与 currentStyle; obj.naturalWidth(nat
clientWidth/clientHeight——视口大小:可见区域的宽度或高度,不包括border、水平滚动条、margin的元素的宽度或高度,只包括元素的内容区域和padding值。 offsetWidth/offsetHeight——视觉大小:看得见的大小,...
很详细的解说了弹弹球的原理,我给的注释很多很详细,便于大家理解。 代码内的注释详细解说了距离...可以很清晰得看到offsetWidth,offsetHeight,document.body.clientWidth,document.clientHeight 这4个属性的用法。
第二组:clientWidth,clientHeight,clientLeft,clientTop 第三组:scrollWidth,scrollHeight,scrollLeft,scrollTop 详细定义如下: 1.1 HTML元素的offsetWidth,offsetHeight以CSS像素返回
在ie6中很多js脚本执行就会出现问题,一般是在获取clientWidth, clientHeight,offsetWidth, offsetHeight,scrollWidth, scrollHeight,scrollTop, scrollLeft会返回不正确的值。 因为在加了doctype后,在ie6中...
网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offsetWidth (包括边线和滚动条的宽); 网页可见区域高: document.body....
maxWidth = smalmg.clientWidth - showimg.offsetWidth; maxHeight = smalmg.clientHeight - showimg.offsetHeight; //上面两个变量指明showimg允许活动的区域 }; smalmg.onmousemove = function(e){ e = e || ...
IE6在页面内容超出窗口大小时将宽度属性scrollWidth、clientWidth、offsetWidth都解释为内容实际宽度。 上次的测试说明了document.body属性并不会给我们返回预期的结果,比如我们用document.body.clientHeight原本想...
var R= document.documentElement.clientWidth-obj.offsetWidth //offsetHeight 则是网页内容实际高度 var B = document.documentElement.clientHeight-obj.offsetHeight // clientHeight 就是透过浏览器看内容的这...
网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offsetWidth (包括边线的宽); 网页可见区域高: document.body.offsetHeight (包括...
clientWidth clientHeight 2、外高度,外宽度: 边框 + 内边距 + 内容框 offsetWidth offsetHeight 3、上边框、左边框 clientTop clientLeft 4、元素的大小及其相对于视口的位置 getBoundingClientRect() //x\y:元素...
width = document.body.clientWidth; height = document.body.clientHeight; Hoffset = dd.offsetHeight; Woffset = dd.offsetWidth; dd.style.left = xPos + document.body.scrollLeft; dd.style.top = yPos + ...
element.clientWidth //元素的实际宽度,不算边框 element.clientHeight //元素的实际高度,不算边框 element.offsetWidth //元素的实际宽度,加上边框 element.offsetHeight //元素的实际高度,加上边框 element....
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWidth (包括边线的宽) 网页可见区域高:document.body.offsetHeight (包括边线的宽)...
s += ” 网页可见区域宽:”+ document.body.clientWidth+” “; s += ” 网页可见区域高:”+ document.body.clientHeight+” “; s += ” 网页可见区域宽:”+ document.body.offsetWidth + ” (包括边线和滚动条...
scrW = document.body.offsetWidth; scrH = document.body.offsetHeight; } var winW, winH; if(window.innerHeight) { // all except IE winW = window.innerWidth; winH = window.innerHeight...
var R= document.body.clientWidth-obj.offsetWidth var B = document.body.clientHeight-obj.offsetHeight obj.style.left = x + document.body.scrollLeft obj.style.top = y + document.body.scrollTop x = x + ...
在一些复杂的页面中经常会用JavaScript处理一些DOM元素的动态效果...offsetWidth clientWidth scrollWidth offsetHeight clientHeight scrollHeight offsetLeft clientLeft scrollLeft offsetTop clientT