日期:2014-05-16  浏览次数:20520 次

简单问题
JavaScript里的scrollLeft,scrollTop,scrollWidth,scrollHeight,offsetHeight,offsetWidth,offsetTop,offsetLeft是作什么用的,我看过DHTML手册,但里面讲得不清楚,有哪位兄弟肯帮我解释一下?

------解决方案--------------------
scrollHeight: 获取对象的滚动高度。
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
offsetWidth:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的宽度
------解决方案--------------------
楼上正解。
------解决方案--------------------
<style>
#div1{
width:100px;
height:200px;
position:absolute;
left:10px;
top:20px;
border:1px solid #888;
overflow:auto;
}
</style>
<div id= "div1 ">
<pre>



dddddddddddddddddddddddddddddddddddddddd










</pre>
</div>

<button onclick= "show(); "> dd </button>

<script language= "javascript ">
function show(){
alert( "scrollHeight= " + div1.scrollHeight + "\nscrollTop= " + div1.scrollTop + "\nscrollLeft= " + div1.scrollLeft + "\nscrollWidth= " + div1.scrollWidth);
alert( "offsetHeight= " + div1.offsetHeight + "\noffsetTop= " + div1.offsetTop + "\noffsetLeft= " + div1.offsetTop + "\noffsetWidth= " + div1.offsetWidth);
}
</script>

看看这个例子
scrollHeight是那个div内容的高度,不是指定的200px这个高度,scrollWidth宽度同理
scrollLeft是那个div横向滚动条向右移动的长度,scrollTop同理(竖直滚动条向下移动的长度)

offsetHeight是那个div相对于父对象的高度,例子中我用的是绝对高度所以一直是200px,offsetWidth同理
offsetLeft是那个div相对于父对象左侧的长度,例子中我用的是绝对定位所以一直是10px,offsetTop同理

------解决方案--------------------
<SCRIPT>
function fnCheckScroll(){
var iNewHeight = oDiv.scrollHeight;
alert( "The value of the scrollHeight property is: "
+ iNewHeight);
}
</SCRIPT>
:
<DIV ID=oDiv STYLE= "overflow:scroll; height=100;
width=250; text-align:left ">
:
</DIV>
<INPUT TYPE=button VALUE= "Check scrollHeight "
onclick= "fnCheckScroll() ">