---
title: "HTML精确定位:scrollLeft,scrollWidth,clientWidth,offset"
author: "java_frog"
date: "2009-05-04T17:33:09Z"
tags: []
slug: "html-scrollleft-scrollwidth-clientwidth-offset"
draft: false
lastmod: "2009-05-04T17:33:09Z"
id: 379138
url: "https://java-frog.iteye.com/blog/379138"
---

HTML:scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解



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 相对文档的水平座标+垂直方向滚动的量 



　　以上主要指IE之中，FireFox差异如下：

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均无关)



　　测试代码：

CODE:&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd](http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)&quot;&gt;

&lt;html xmlns=&quot;[http://www.w3.org/1999/xhtml](http://www.w3.org/1999/xhtml)&quot; lang=&quot;gb2312&quot;&gt;

&lt;head&gt;

&lt;head&gt;

&lt;title&gt; 代码实例：关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较 &lt;/title&gt;

&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=gb2312&quot; /&gt;

&lt;meta name=&quot;author&quot; content=&quot;枫岩,CnLei.y.l@gmail.com&quot;&gt;

&lt;meta name=&quot;copyright&quot; content=&quot;[http://www.cnlei.com](http://www.cnlei.com)&quot; /&gt;

&lt;meta name=&quot;description&quot; content=&quot;关于clientWidth、offsetWidth、clientHeight、offsetHeight的测试比较&quot; /&gt;

&lt;style type=&quot;text/css&quot; media=&quot;all&quot;&gt;

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;}

&lt;/style&gt;

&lt;script type=&quot;text/javascript&quot;&gt;

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;

}

&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;p&gt;点击下面的链接:&lt;/p&gt;

&lt;div id=&quot;Div_CnLei&quot;&gt;

&lt;p&gt;&lt;a href=&quot;javascript:alert(GetClientWidth('Div_CnLei'));&quot;&gt;GetClientWidth();&lt;/a&gt; &lt;a href=&quot;javascript:alert(GetClientHeight('Div_CnLei'));&quot;&gt;GetClientHeight();&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;javascript:alert(GetOffsetWidth('Div_CnLei'));&quot;&gt;GetOffsetWidth();&lt;/a&gt; &lt;a href=&quot;javascript:alert(GetOffsetHeight('Div_CnLei'));&quot;&gt;GetOffsetHeight();&lt;/a&gt;&lt;/p&gt;

&lt;/div&gt;

&lt;div id=&quot;Description&quot;&gt;

&lt;p&gt;&lt;strong&gt;IE6.0、FF1.06+：&lt;/strong&gt;&lt;br /&gt;

clientWidth = width + padding = 300+10×2 = 320&lt;br /&gt;

clientHeight = height + padding = 200+10×2 = 220&lt;br /&gt;

offsetWidth = width + padding + border = 300+10×2+10×2= 340&lt;br /&gt;

offsetHeight = height + padding + border = 200+10×2+10×2 = 240&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IE5.0/5.5：&lt;/strong&gt;&lt;br /&gt;

clientWidth = width - border = 300-10×2 = 280&lt;br /&gt;

clientHeight = height - border = 200-10×2 = 180&lt;br /&gt;

offsetWidth = width = 300&lt;br /&gt;

offsetHeight = height = 200&lt;/p&gt;

&lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;