JS中offsetLeft与scrollLeft区别 clientWidth、offsetWidth和scrollWidth

offsetLeft:Html元素相对于自己的offsetParent元素的位置

scrollLeft:返回和设置当前横向滚动务的坐标值

<input type="button" value="点一下" onclick="move()">
<div id="d" style="background-color:#ff9966; position:absolute; left:170px; top:100px;width:300;height:300;overflow:scroll"
onclick="alert('offsetLeft:'+this.offsetLeft)">
<div style="height:600;width:600" onclick="alert('offsetLeft:'+this.offsetLeft)"></div>
</div>
<script language="javascript">
function move()
{
var d=document.getElementById("d")
a=eval(20)
d.scrollLeft+=a
}
</script>

保存为网页,运行一下,点按钮,滚动条移动
点击div,先弹出b相对于a的位置,再弹出a相对于窗口的位置

 

 

<meta http-equiv="Content-Type" Content="text/html; charset=utf-8">
<textarea wrap="off" onmouseover=
"alert(' clientWidth:'+this.clientWidth+
'\n offsetWidth:'+this.offsetWidth+'\n scrollWidth:'+this.scrollWidth);">
11111111111111111111111111111
< /textarea>
clientWidth是对象看到的宽度(不含边线)
offsetWidth是对象看到的宽度(含边线,如滚动条的占用的宽,值会随着内容的输入而不断改变)
scrollWidth是对象实际内容的宽度。

posted @ 2008-12-01 14:09  XGU_Winner  阅读(778)  评论(0编辑  收藏  举报