Javascript:scrollWidth,clientWidth,offsetWidth的区别

 

网页可见区域宽:document.body.clientWidth;   
网页可见区域高:document.body.clientHeight;   
网页可见区域高:document.body.offsetWeight:   
网页可见区域高: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;

scrollWidth
是对象的实际内容的宽,不包边线宽度,会随对象中内容的多少改变(内容多了可能会改变对象的实际宽度)

clientWidth
是对象可见的宽度,不包滚动条等边线,会随窗口的显示大小改变。

offsetWidth
是对象的可见宽度,包滚动条等边线,会随窗口的显示大小改变。


------------------------------------------------
一个scrollWidth和clientWidth的例子:
<html>
<head>
<title>77.htm文件</title>
</head>

<body>
<textarea wrap="off" οnfοcus="alert('scrollWidth:'+this.scrollWidth+'/n clientWidth:'+this.clientWidth);"></textarea>
</body>
</html>
在文本框内输入内容,当横向滚动条没出来前scrollWidth和clientWidth的值是一样的。
当一行内容超出文本框的宽度,就有横向滚动条出来了,scrollWidth的值就变了。
scrollWidth是对象实际内容的宽度。
clientWidth是对象看到的宽度(不含边线),这个例子里不会改变。


-----------------------------------------------
一个clientWidth和offsetWidth的例子:
<html>
<head>
<title>77.htm文件</title>
</head>

<body>
<textarea wrap="off" οnfοcus="alert('offsetWidth:'+this.offsetWidth+'/n clientWidth:'+this.clientWidth);"></textarea>
</body>
</html>

offsetWidth的值总是比clientWidth的值打
clientWidth是对象看到的宽度(不含边线)
offsetWidth是对象看到的宽度(含边线,如滚动条的占用的宽)

top、postop、scrolltop、scrollHeight、offsetHeight

1. top

此属性仅仅在对象的定位(position)属性被设置时可用。否则,此属性设置会被忽略。

< div style ="background-color:red; position:absolute; width:100px; height:100px;" >

< p style ="background-color:silver; position:absolute; top:-5px;" > 测试top </ p >

</ div >

上面是一个段落P包含在一个DIV内,可以看到P的top设置为-5px后,它的上边距超过了容器DIV的上边距,超过的这段距离就是设置的5px。

需要注意的是,DIV和P这一对包含元素,都需要设置position为absolute才能得到想要的结果,假如父元素不设置,则子元素的参照将是更上层定义过position的元素,直到整个文档;

2. posTop

posTop的数值其实和top是一样的,但区别在于,top固定了元素单位为px,而posTop只是一个数值(这一点可以通过alert("top="+id.style.top)和alert("posTop="+id.style.posTop)来证明),因此一般使用posTop来进行运算。

< div style ="background-color:red; position:absolute; width:100px; height:100px;" >

< p id ="test" style ="background-color:silver; position:absolute;" > 测试posTop </ p >

</ div >

< script >
test.style.posTop
= 15 + 8 ;
alert(
" top= " + test.style.top);
alert(
" posTop= " + test.style.posTop);
</ script >

无论你使用top或posTop来赋值,最后的结果都是一致的

3. scrollTop

< div id ="container" style ="background-color:silver; width:100px; height:100px; overflow:auto;" >
< p style ="background-color:red;" >
别再做情人 做只猫 做只狗 不做情人 做只宠物至少可爱迷人 和你相交不浅无谓明日会被你憎
</ p >
</ div >

< script >
container.scrollTop
= 12 ;
</ script >

这一段文本在这个100*100的DIV内无法完全显示,所以设置了overflow为auto,它会出现一个上下方向的滑动框,假如没有设置id.scrollTop属性的话,默认情况下滑块位置在顶端。而设置了scrollTop值为12后,滑块的位置改变了,默认显示是卷过了12个象素的文本。如果设置overflow为hidden,则将会无法显示顶部12个象素的文本。

注意设置方式是id.scrollTop,而不是id.style.scrollTop。

4. scrollHeight 与 offsetHeight

offsetHeight是自身元素的高度,scrollHeight是 自身元素的高度+隐藏元素的高度。

< div id ="container" style ="background-color:silver; width:100px; height:100px; overflow:auto;" >
< p style ="background-color:red; height:250px; " >
别再做情人 做只猫 做只狗 不做情人 做只宠物至少可爱迷人 和你相交不浅无谓明日会被你憎
</ p >
</ div >

< script >
alert(container.offsetHeight);
alert(container.scrollHeight);
</ script >

将依次输出100,250。因为已经指定了元素的height为100px,所以offsetHeight始终为100px;内部元素为250px,而容器元素只有100px,那么还有150px的内容它无法显示出来,但它却是实际存在的,所以scrollHeight值为100+150=250。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值