標籤:方向 滾動 擷取對象 strong name scrollto setw 位置 eve
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:
[Copy to clipboard]
<!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="楓岩,[email protected]">
<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=http://bbs.chinaunix.net/"javascript:alert(GetClientWidth(‘Div_CnLei‘));">GetClientWidth();</a> <a href=http://bbs.chinaunix.net/"javascript:alert(GetClientHeight(‘Div_CnLei‘));">GetClientHeight();</a></p>
<p><a href=http://bbs.chinaunix.net/"javascript:alert(GetOffsetWidth(‘Div_CnLei‘));">GetOffsetWidth();</a> <a href=http://bbs.chinaunix.net/"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>
HTML控制項offsetTop、scrollTop等屬性
假設 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 解釋不同造成的)。
js offsetHeight offsetWidth 解說