offsetTop、clientTop、offsetWidth、clientWidth、scrollWidth、scrollTop、scrollLeft区别

 demo1

View Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>scrollWidth,scrollHeight,scrollTop,scrollLeft,offsetWidht,offsetHeight,offsetTop,offsetLeft,clientWidht,clientHeight,clientTop,clientLeft</title>
    <script language="javascript">
        function getOjectAttribute(param)
        {
              var obj = document.getElementById("TestDiv");
              var num;
           switch(param)
           {
              case "scrollWidth":
                  alert(obj.scrollWidth);
             break;
           case "scrollHeight":
                  alert(obj.scrollHeight);
             break;
            
           case "offsetWidth":
                  alert(obj.offsetWidth);
             break;
           case "offsetHeight":
                  alert(obj.offsetHeight);
             break;
            
           case "clientWidth":
                  alert(obj.clientWidth);
             break;
           case "clientHeight":
                  alert(obj.clientHeight);
             break;
          
           case "scrollLeft":
                  alert(obj.scrollLeft);
             break;
           case "scrollTop":
                  alert(obj.scrollTop);
             break;
            
           case "offsetLeft":
                  alert(obj.offsetLeft);
             break;
           case "offsetTop":
                  alert(obj.offsetTop);
             break;
            
            
           case "clientLeft":
                  alert(obj.clientLeft);
             break;
           case "clientTop":
                  alert(obj.clientTop);
             break;
           }
        }
        
        function SetDiv()
        {
             var obj = document.getElementById("TestDiv");
        var result="";
        var wd = document.getElementById("widthtext").value;
        if(wd!="")
        {
             obj.style.width=wd;
        }
        
        var ht = document.getElementById("heighttext").value;
        if(ht!="")
        {
             obj.style.height=ht;
        }
        
        var mgleft = document.getElementById("mglefttext").value;
        if(mgleft!="")
        {
             obj.style.marginLeft=mgleft;
        }
        
        var mgright = document.getElementById("mgrighttext").value;
        if(mgright!="")
        {
             obj.style.marginRight=mgright;
        }
        
        var mgtop = document.getElementById("mgtoptext").value;
        if(mgtop!="")
        {
             obj.style.marginTop=mgtop;
        }
        
        var mgbottom = document.getElementById("mgbottomtext").value;
        if(mgbottom!="")
        {
             obj.style.marginBottom=mgbottom;
        }
        
        var pdbottom = document.getElementById("pdbottomtext").value;
        if(pdbottom!="")
        {
             obj.style.paddingBottom=pdbottom;
        }
        
        var pdleft = document.getElementById("pdlefttext").value;
        if(pdleft!="")
        {
             obj.style.paddingLeft=pdleft;
        }
        
        var pdright = document.getElementById("pdrighttext").value;
        if(pdright!="")
        {
             obj.style.paddingRight=pdright;
        }
        var pdtop = document.getElementById("pdtoptext").value;
        if(pdtop!="")
        {
             obj.style.paddingTop=pdtop;
        }
        
        var info = document.getElementById("divinfo").value;
        if(info!="")
        {
             obj.innerHTML=info;
        }
        
        var overflows = document.all["rddisplay"];
        for(var i=0;i<overflows.length;i++)
        {
              if(overflows[i].checked)
           {
               obj.style.overflow = overflows[i].value;
           }
        }
        result+="TestDiv:width="+obj.style.pixelWidth+"--height="+obj.style.pixelHeight+"<br>";
        result+="scroll:scrollWidth="+obj.scrollWidth+"---scrollHeight="+obj.scrollHeight+"---scrollLeft="+obj.scrollLeft+"---scrollTop="+obj.scrollTop+"<br>";
        result+="offset:offsetWidth="+obj.offsetWidth+"---offsetHeight="+obj.offsetHeight+"---offsetLeft="+obj.offsetLeft+"---offsetTop="+obj.offsetTop+"<br>";
        result+="client:clientWidth="+obj.clientWidth+"---clientHeight="+obj.clientHeight+"---clientLeft="+obj.clientLeft+"---clientTop="+obj.clientTop+"<br>";
        
        document.getElementById("ResultDiv").innerHTML=result;
        document.getElementById("ResultDiv").style.fontWeight="bold";
        }
        
        function ScrollDiv()
        {
             var obj = document.getElementById("TestDiv");
        var vscrollHeight = obj.scrollHeight;
        var vclientHeight = obj.clientHeight;
        var vscrollTop = obj.scrollTop;
        if(vscrollHeight>vclientHeight)
        {
             vscrollTop=vscrollTop+5;
             obj.scrollTop=vscrollTop;
        }
        SetDiv();
        if((vscrollHeight-obj.scrollTop)==obj.clientHeight)
        {
             obj.scrollTop=0;
           }
        setTimeout("ScrollDiv()",50);
        }
    </script>
</head>

<body>
    <form name="form1">
        <div id="TestDiv" style=" background-color:#CC0000;">this is test project for study javascript</div>
        <input type="button" value="GetscrollWidth" onclick="getOjectAttribute('scrollWidth');"/>
        <input type="button" value="GetscrollHeight" onclick="getOjectAttribute('scrollHeight');"/>
        <input type="button" value="GetoffsetWidth" onclick="getOjectAttribute('offsetWidth');"/>
        <input type="button" value="GetoffsetHeight" onclick="getOjectAttribute('offsetHeight');"/>
        <input type="button" value="GetclientWidth" onclick="getOjectAttribute('clientWidth');"/>
        <input type="button" value="GetclientHeight" onclick="getOjectAttribute('clientHeight');"/>
        <br />
        <input type="button" value="GetscrollLeft" onclick="getOjectAttribute('scrollLeft');"/>
        <input type="button" value="GetscrollTop" onclick="getOjectAttribute('scrollTop');"/>
        <input type="button" value="GetoffsetLeft" onclick="getOjectAttribute('offsetLeft');"/>
        <input type="button" value="GetoffsetTop" onclick="getOjectAttribute('offsetTop');"/>
        <input type="button" value="GetclientLeft" onclick="getOjectAttribute('clientLeft');"/>
        <input type="button" value="GetclientTop" onclick="getOjectAttribute('clientTop');"/>
        <br />
        <input type="button" value="ScrollTheDiv" onclick="ScrollDiv();"/>
        <br /><br />
        <fieldset>
            Width:<input type="text" id="widthtext" /><br />
            Height:<input type="text" id="heighttext"/><br />
            Margin-Left:<input type="text" id="mglefttext" size="5" />
            Margin-Right:<input type="text" id="mgrighttext" size="5" />
            Margin-Top:<input type="text" id="mgtoptext" size="5" />
            Margin-Bottom:<input type="text" id="mgbottomtext" size="5" />
            <br />
            Padding-Left:<input type="text" id="pdlefttext" size="5" />
            Padding-Right:<input type="text" id="pdrighttext" size="5" />
            Padding-Top:<input type="text" id="pdtoptext" size="5" />
            Padding-Bottom:<input type="text" id="pdbottomtext" size="5" />
            <br />
            OverFlow:<input type="radio" name="rddisplay" value="auto"/>auto
            <input type="radio" name="rddisplay" value="hidden"/>hidden
            <input type="radio" name="rddisplay" value="inherit"/>inherit
            <input type="radio" name="rddisplay" value="scroll"/>scroll
            <input type="radio" name="rddisplay" value="scroll"/>visible
            <br />
            innerText:
            <br />
            <textarea name="divinfo" id="divinfo" cols="100" rows="10"></textarea>
            <br />
            <input type="button" value="BuildStyle" onclick="SetDiv();"/>
            <input type="reset" value="reset" />
            </fieldset>
            <br />
        <fieldset>
            Result:<div id="ResultDiv"></div>
        </fieldset>
        <br />
    </form>
</body>
</html>

demo2

View Code

demo3

View Code
  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5     <title>offset position scrollTop clientTop</title>
  6     <style type="text/css">
  7         <!--
  8         div{
  9         font-family: "宋体";
 10         font-size: 12px;
 11         color: #000000;
 12         }
 13         #div1{
 14         position:absolute;
 15         background-color:#f0f0f0;
 16         width:200px;
 17         height:200px;
 18         left:20px;
 19         top:0px;
 20         z-index:1;
 21         }
 22         #div2{
 23         background-color:#cfc0cf;
 24         width:200px;
 25         height:210px;
 26         position:absolute;
 27         left:261px;
 28         top:347px;
 29         z-index:100;
 30         }
 31         #div3{
 32         background-color:#abbfbf;
 33         width:200px;
 34         height:200px;
 35         position:absolute;
 36         left:20px;
 37         top:247px;
 38         z-index:100;
 39         }
 40         #div4{
 41         background-color:#cfcfcf;
 42         width:200px;
 43         height:200px;
 44         position:absolute;
 45         left:461px;
 46         top:147px;
 47         z-index:100;
 48         }
 49         -->
 50     </style>
 51     <script>
 52         function offset(ids){
 53             var ev=event || window.event;
 54             ids.innerHTML=ids.id+"<br/>";
 55             ids.innerHTML+="offsetLeft ="+ids.offsetLeft+"<BR>";
 56             ids.innerHTML+="offsetWidth ="+ids.offsetWidth+"<BR>";
 57             ids.innerHTML+="offsetHeight ="+ids.offsetHeight+"<BR>";
 58             ids.innerHTML+="offsetTop ="+ids.offsetTop+"<BR>";
 59             ids.innerHTML+="event.offset 鼠标相对于控件的位置<BR>";
 60             ids.innerHTML+="offsetX ="+ev.offsetX+"<BR>";
 61             ids.innerHTML+="offsetY ="+ev.offsetY+"<BR>";
 62         }
 63         function screen(ids){
 64             ids.innerHTML=ids.id+"<br/>";
 65             ids.innerHTML+="scrollWidth ="+ids.scrollWidth+"<BR>";
 66             ids.innerHTML+="scrollHeight ="+ids.scrollHeight+"<BR>";
 67             ids.innerHTML+="scrollTop ="+ids.scrollTop+"<BR>";
 68             ids.innerHTML+="scrollLeft ="+ids.scrollLeft+"<BR>";
 69         }
 70         function client(ids){
 71             ids.innerHTML=ids.id+"<br/>";
 72             ids.innerHTML+="clientWidth ="+ids.clientWidth+"<BR>";
 73             ids.innerHTML+="clientHeight ="+ids.clientHeight+"<BR>";
 74             ids.innerHTML+="clientTop ="+ids.clientTop+"<BR>";
 75             ids.innerHTML+="clientLeft ="+ids.clientLeft+"<BR>";
 76         }
 77         function eventc(ids,event){
 78             var ev=event || window.event;
 79             //if (!ev)var ev = this.getEvent.caller;
 80             ids.innerHTML=ids.id+"<br/>";
 81             ids.innerHTML+="鼠标相对于屏幕坐标<BR>event.screenX="+ev.screenX+"<BR>";
 82             ids.innerHTML+="event.screenY ="+ev.screenY+"<BR>";
 83             ids.innerHTML+="鼠标相对于网页坐标event.clientX="+ev.clientX+"<BR>";
 84             ids.innerHTML+="event.clientY ="+ev.clientY+"<BR>";
 85         }
 86     </script>
 87 </head>
 88 
 89 <body>
 90     <div id='div1' onmouseMove='eventc(this,event)'>div1</div>
 91     <div id='div2' onclick='client(this);'>div2</div>
 92     <div id='div3' onclick='screen(this);'>div3</div>
 93     <div id='div4'onclick='offset(this);'>offset 控件相对于父窗体的位置</div>
 94     <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
 95     <ul>
 96         <li>div{font-family: "宋体";font-size: 12px;color: #000000;}</li>
 97         <li>div1{position:absolute;background-color:#f0f0f0;200px;height:200px;left:20px;top:0px;z-index:1;}</li>
 98         <li>div2{background-color:#cfc0cf;200px;height:210px;position:absolute;left:261px;top:347px;z-index:100;}</li>
 99         <li>div3{background-color:#abbfbf;200px;height:200px;position:absolute;left:20px;top:247px;z-index:100;}</li>
100         <li>div4{background-color:#cfcfcf;200px;height:200px;position:absolute;left:461px;top:147px;z-index:100;}</li>
101     </ul>
102 </body>
103 </html>
1.概念
offsetWidth/offsetHeight(非标准属性,但各浏览器都支持)
   对象的可见宽度,包含滚动条和border。
clientWidth/clientHeight
   对象的可见宽度,不包含滚动条和border。
scrollWidth/scrollHeight(慎用)
   元素完整的高度和宽度,overflow:hidden的部分也计算在内。
offsetLeft/offsetTop
   当前元素距浏览器边界的偏移量,以像素为单位。
clientTop/clientLeft
   这个属性测试下来的结果是=border。
scrollLeft/scrllTop(IE6计算方式不同)
   设置或返回已经滚动到元素的左边界或上边界的像素数。
2.比较offsetHeight/clientHeight/scrollHeight
共同点:3个值都和元素的margin无关。
差异:offsetHeight=height+padding+border
     clientHeight=height+padding-滚动条的宽度(如果有滚动条)
     scrollHeight 获得的是元素的实际宽度影藏的部分也计算在内
备注:1.Jquery中的css("height")/height()不计算padding/border/滚动条。
     2.offsetWidth、clientWidth、scrollWidth同理。
3.比较offsetTop/clientTop/scrollTop
offsetTop:元素相对body的Top值(元素border以外到body.padding以内)
clientTop=offsetTop-(height+padding)=border
scrollTop:元素的滚动值


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

网页可见区域宽: document.body.clientWidth;
网页可见区域高: document.body.clientHeight;
网页可见区域宽: document.body.offsetWidth   (包括边线的宽);
网页可见区域高: document.body.offsetHeight (包括边线的宽);
网页正文全文宽: document.body.scrollWidth;
网页正文全文高: document.body.scrollHeight;
网页被卷去的高: document.body.scrollTop;
网页被卷去的左: document.body.scrollLeft;
网页正文部分上: window.screenTop;
网页正文部分左: window.screenLeft;
屏幕分辨率的高: window.screen.height;
屏幕分辨率的宽: window.screen.width;
屏幕可用工作区高度: window.screen.availHeight;
屏幕可用工作区宽度:window.screen.availWidth;


scrollHeight: 获取对象的滚动高度。 
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 
scrollWidth:获取对象的滚动宽度 
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度 
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置 
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置 
event.clientX 相对文档的水平座标 
event.clientY 相对文档的垂直座标 
event.offsetX 相对容器的水平坐标 
event.offsetY 相对容器的垂直坐标 
document.documentElement.scrollTop 垂直方向滚动的值 
event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量

1、offsetLeft

假设 obj 为某个 HTML 控件。
obj.offsetTop 指 obj 距离上方或上层控件的位置,整型,单位像素。
obj.offsetLeft 指 obj 距离左方或上层控件的位置,整型,单位像素。
obj.offsetWidth 指 obj 控件自身的宽度,整型,单位像素。
obj.offsetHeight 指 obj 控件自身的高度,整型,单位像素。
我们对前面提到的“上方或上层”与“左方或上层”控件作个说明。
例如:
<div id="tool">
<input type="button" value="提交">
<input type="button" value="重置">
</div>
“提交”按钮的 offsetTop 指“提交”按钮距“tool”层上边框的距离,因为距其上边最近的是 “tool” 层的上边框。
“重置”按钮的 offsetTop 指“重置”按钮距“tool”层上边框的距离,因为距其上边最近的是 “tool” 层的上边框。
“提交”按钮的 offsetLeft 指“提交”按钮距“tool”层左边框的距离,因为距其左边最近的是 “tool” 层的左边框。
“重置”按钮的 offsetLeft 指“重置”按钮距“提交”按钮右边框的距离,因为距其左边最近的是“提交”按钮的右边框。
以上属性在 FireFox 中也有效。

另 外:我们这里所说的是指 HTML 控件的属性值,并不是 document.body,document.body 的值在不同浏览器中有不同解释(实际上大多数环境是由于对 document.body 解释不同造成的,并不是由于对 offset 解释不同造成的),不同点:
  标题:offsetTop 与 style.top 的区别

  预备知识:offsetTop、offsetLeft、offsetWidth、offsetHeight
  我们知道 offsetTop 可以获得 HTML 元素距离上方或外层元素的位置,style.top 也是可以的,二者的区别是:
  一、offsetTop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。
  二、offsetTop 只读,而 style.top 可读写。
  三、如果没有给 HTML 元素指定过 top 样式,则 style.top 返回的是空字符串。

  offsetLeft 与 style.left、offsetWidth 与 style.width、offsetHeight 与 style.height 也是同样道理。


  标题:clientHeight、offsetHeight和scrollHeight
    我们这里说说四种浏览器对 document.body 的 clientHeight、offsetHeight 和 scrollHeight 的解释,这里说的是 document.body,如果是 HTML 控件,则又有不同。
    这四种浏览器分别为IE(Internet Explorer)、NS(Netscape)、Opera、FF(FireFox)。

2、clientHeight
大家对 clientHeight 都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。
  *offsetHeight
  IE、Opera 认为 offsetHeight = clientHeight + 滚动条 + 边框。
  NS、FF 认为 offsetHeight 是网页内容实际高度,可以小于 clientHeight。
  *scrollHeight
  IE、Opera 认为 scrollHeight 是网页内容实际高度,可以小于 clientHeight。
  NS、FF 认为 scrollHeight 是网页内容高度,不过最小值是 clientHeight。
简单地说
  clientHeight 就是透过浏览器看内容的这个区域高度
  NS、 FF 认为 offsetHeight 和 scrollHeight 都是网页内容高度,只不过当网页内容高度小于等于 clientHeight 时,scrollHeight 的值是 clientHeight,而     offsetHeight 可以小于 clientHeight。
  IE、Opera 认为 offsetHeight 是可视区域 clientHeight 滚动条加边框。scrollHeight 则是网页内容实际高度。
同理
clientWidth、offsetWidth 和 scrollWidth 的解释与上面相同,只是把高度换成宽度即可。

 元素的可视部分的宽度和高度(也就是CSS的width加padding)。它们不把边框和滚动条计算在内,也不包括任何可能的滚动。若CSS中没有指定元素的高度和宽度(即自适应),则IE中显示0,而非IE浏览器则显示实际的值

但是
FF 在不同的 DOCTYPE 中对 clientHeight 的解释不同, xhtml 1 trasitional 中则不是如上解释的。其它浏览器则不存在此问题。
标题:scrollTop、scrollLeft、scrollWidth、scrollHeight

3、scrollLeft
scrollTop 是“卷”起来的高度值,示例:

1 <div style="100px;height:100px;background-color:#FF0000;overflow:hidden;" id="p">
2   <div style="50px;height:300px;background-color:#0000FF;" id="t">如果为 p 设置了 scrollTop,这些内容可能不会完全显示。</div>
3 </div>
4 <script type="text/javascript">
5   var p = document.getElementById("p");
6   p.scrollTop = 10;
7 </script>

由于为外层元素 p 设置了 scrollTop,所以内层元素会向上卷。

scrollLeft 也是类似道理
我们已经知道 offsetHeight 是自身元素的宽度
而 scrollHeight 是内部元素的绝对宽度,包含内部元素的隐藏的部分。
上述中 p 的 scrollHeight 为 300,而 p 的 offsetHeight 为 100。
scrollWidth 也是类似道理。
IE 和 FireFox 全面支持,而 Netscape 和 Opera 不支持 scrollTop、scrollLeft(document.body 除外)。
标题:offsetTop、offsetLeft、offsetWidth、offsetHeight

4、clientLeft  

返回对象的offsetLeft属性值和到当前窗口左边的真实值之间的距离,可以理解为边框的长度
一直以来对offsetLeft,offsetTop,scrollLeft,scrollTop这几个方法很迷糊,花了一天的时间好好的学习了一下.得出了以下的结果:
1.offsetTop     :
  当前对象到其上级层顶部的距离.
  不能对其进行赋值.设置对象到页面顶部的距离请用style.top属性.
2.offsetLeft :
  当前对象到其上级层左边的距离.
  不能对其进行赋值.设置对象到页面左部的距离请用style.left属性.
3.offsetWidth :
  当前对象的宽度.
  与style.width属性的区别在于:如对象的宽度设定值为百分比宽度,则无论页面变大还是变小,style.width都返回此百分比,而offsetWidth则返回在不同页面中对象的宽度值而不是百分比值
4.offsetHeight :
  与style.height属性的区别在于:如对象的宽度设定值为百分比高度,则无论页面变大还是变小,style.height都返回此百分比,而offsetHeight则返回在不同页面中对象的高度值而不是百分比值
5.offsetParent   :
  当前对象的上级层对象.
  注意.如果对象是包括在一个DIV中时,此DIV不会被当做是此对象的上级层,(即对象的上级层会跳过DIV对象)上级层是Table时则不会有问题.
  利用这个属性,可以得到当前对象在不同大小的页面中的绝对位置.
得到绝对位置脚本代码

 1 function GetPosition(obj){
 2  var left = 0;
 3  var top   = 0;
 4  while(obj != document.body){
 5         left = obj.offsetLeft;
 6         top   = obj.offsetTop;
 7 
 8         obj = obj.offsetParent;
 9  }
10  alert("Left Is : " + left + "\r\n" + "Top   Is : " + top);
11 }

6.scrollLeft :

  对象的最左边到对象在当前窗口显示的范围内的左边的距离.
  即是在出现了横向滚动条的情况下,滚动条拉动的距离.
7.scrollTop
  对象的最顶部到对象在当前窗口显示的范围内的顶边的距离.
  即是在出现了纵向滚动条的情况下,滚动条拉动的距离.

转自:http://hi.baidu.com/sonan/blog/item/6d97d8092a669389d0581bf7.html

参考:http://www.zhumin.me/archives/269

原文地址:https://www.cnblogs.com/wen12128/p/2513167.html