页面位置 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHe

1.top

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

代码如下:

<div style=" position:absolute; width:100px; height:100px;">
<p style=" 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只是一个数值,一般使用posTop来进行运算。

3.scrollTop

代码如下:

<div id="container" style=" width:100px; height:100px; overflow:auto;">
<p style="" mce_style="">
这里是文本
</p>
</div>
<script type="text/javascript"><!--
container.scrollTop = 12;
// --></script>


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

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

clientHeight
大家对 clientHeight 都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看
到内容的这个区域的高度。

offsetHeight
IE、Opera 认为 offsetHeight = clientHeight + 滚动条 + 边框。
NS、FF 认为 offsetHeight 是网页内容实际高度,可以小于 clientHeight。

scrollHeight
IE、Opera 认为 scrollHeight 是网页内容实际高度,可以小于 clientHeight。
NS、FF 认为 scrollHeight 是网页内容高度,不过最小值是 clientHeight。

简单地说
clientHeight 就是透过浏览器看内容的这个区域高度。
NS、FF 认为 offsetHeight 和 scrollHeight 都是网页内容高度,只不过当网页内容高度小于等于
 clientHeight 时,scrollHeight 的值是 clientHeight,而 offsetHeight 可以小于 clientHeight。
IE、Opera 认为 offsetHeight 是可视区域 clientHeight 滚动条加边框。
scrollHeight 则是网页内容实际高度。

同理
clientWidth、offsetWidth 和 scrollWidth 的解释与上面相同,只是把高度换成宽度即可。

说了半天还是给个图吧

 

那么当容器如div中的table的宽大与定义的div的宽的时候,这个时候ScrollLeft++是可以起作用的!
example

 

Code

 

 

而这里的 document.getElementById("TextBox1").scrollTop++;是不能起作用的!为什么呢?因为他的容器的
高scrollHeight不大于定义的 <asp:TextBox ID="TextBox1" runat="server" Height="50px">中的50px
如果大于就可以起作用了,如
<body>
    <form id="form1" runat="server">
    <div id=demo>
      
        <asp:TextBox ID="TextBox1" runat="server" Height="24px" TextMode="MultiLine" Width="30px" Wrap="False">
        fghdsfgfdgdf
guj</asp:TextBox>
        <input id="Button1" type="button" value="button" onclick="scrolltop1()" /></div>
    <script language="javascript" type="text/javascript">
  function scrolltop1()
  {
      for(var i=1;i<500;i++)
      {
          document.getElementById("TextBox1").scrollTop++;
          document.getElementById("TextBox1").scrollLeft++;
      }
  window.alert(document.getElementById("TextBox1").scrollWidth);
  window.alert(document.getElementById("TextBox1").scrollLeft);
  }
    </script>
    </form>
</body>
这个时候,scrollleft和scrolltop都是可以起作用的,这里给我们一起提示
要想用这两个家伙就必须把实际的内容弄得多于定义的容器的width和height

下面给出横向移动的代码

 

Code

 

在给出竖向移动的代码

 

Code

 

其实代码是比较简单的,只是要理解其意思就可以了!

4.offsetTop

如果元素 A 是 HTML 的 body 元素,其 display 属性计算值是 none,或者不具有 CSS 布局盒子,则返回 0,并停止本算法。

如果元素 A 的 offsetParent 是 null 或者是 HTML 的 body 元素,以 CSS 像素为单位返回元素 A 上边框距画布原点的垂直距离,并停止本算法。

以 CSS 像素为单位返回元素 A 上边框距其 offsetParent 上边框的距离。

5. scrollHeight 与 offsetHeight与clientHeight

对于document.body

clientHeight
大家对 clientHeight 都没有什么异议,都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。

offsetHeight
IE、Opera 认为 offsetHeight = clientHeight + 滚动条 + 边框。
NS、FF 认为 offsetHeight 是网页内容实际高度,可以小于 clientHeight。

scrollHeight
IE、Opera 认为 scrollHeight 是网页内容实际高度,可以小于 clientHeight。
NS、FF 认为 scrollHeight 是网页内容高度,不过最小值是 clientHeight

对某个HTML控件

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

代码如下:

<div id="container" style=" width:100px; height:100px; overflow:auto;">
<p style=" height:250px; ">
别再做情人 做只猫 做只狗 不做情人 做只宠物至少可爱迷人 和你相交不浅无谓明日会被你憎</p>
</div>
<script type="text/javascript"><!--
alert(container.offsetHeight);
alert(container.scrollHeight);
// --></script>


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

时间: 2024-08-14 03:43:28

页面位置 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHe的相关文章

javascript中top,clientTop,scrollTop,offsetTop用法

网页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offsetWidth     (包括边线的宽); 网页可见区域高: document.body.offsetHeight   (包括边线的宽); 网页正文全文宽: document.body.scrollWidth; 网页正文全文高: document.body.scrollHeight; 网页被卷去的高

理解clientX、clientY、offsetLeft、event.offsetTop、offsetWidth、offsetHeight、clientWidth、clientHeight、scrollTop、scrollHeight

一.clientX和clientY 事件发生时,鼠标距离浏览器的可视区域的X.Y轴的位置,不包含滚动条的区域的部分.就算是页面进行了滚动,鼠标的坐标值还是参考可视区域的. 二.offsetLeft和offsetTop 事件源元素相对于父节点的偏移的像素值. 三.offsetWidth和offsetHeight 获取的是元素的宽度,包含border,padding,内容宽度,以及滚动条的宽度,和element.getBoundingClientRect()的值是一致的. 四.clientWidth

各种位置和高度计算:.position()、.offset()、.outerHeight()、.scrollTop、.scrollHeight、.clientHeight

1..position()和.offset() jquery的.position()获取相对于最近的position为relative或absolute的父元素的偏移,返回.position().left和.position().top,不算上自己的margin-left: jquery的.offset()获取相对于视口左上角的偏移,返回.offset().left和.offset().top, 算上自己的margin-left,,还可以设置.offset({left:,top:});这个很有用

js 位置属性offsetLeft,offsetTop,scrollLeft,scrollTop等测试

一直对这几个属性,有点模糊,今天有时间,好好研究一下. 一下上网上前辈写的关于这方面的博客链接: http://blog.csdn.net/wgw335363240/article/details/5580654 http://blog.csdn.net/fswan/article/details/17238933 看了几篇之后,有点小懂,还是亲身测试一下比较好,实践是检验真理的唯一标准.嘿嘿!! 下面是我写的demo的截图.这个demo上列出了,js的常用位置属性,可以拖拽.缩放元素,来观察各

js的onscroll、scrollTop、scrollHeight及window.scroll等方法

onscroll 解释:当元素的滚动条滚动时触发的事件. onscroll事件貌似任何实体元素都可以绑定,这里的实体元素包括DOM元素.window元素.document元素. 用法即:element.onscroll=function(){}; 需要注意的是,滚动条一定要出现,而且滚动条是属于这元素的,例如: <div id="wrap" style="height:100px;overflow:auto;"> <div id="inn

container.scrollTop = container.scrollHeight ???

最近在用node.js做一款聊天室的时候,网上查资料发现一行js代码,不是很清楚是干嘛的,就来搞一搞. 1 _displayNewMsg: function(user, msg, color){ 2 var container = document.getElementById('historyMsg'), 3 msgToDisplay = document.createElement('p'), 4 date = new Date().toTimeString().substr(0,8); 5

深入理解定位父级offsetParent及偏移大小offsetTop / offsetLeft / offsetHeight / offsetWidth

深入理解定位父级offsetParent及偏移大小 [转载] 前面的话 偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.offsetTop.offsetHeight.offsetWidth这四个属性.当然,还有一个偏移参照——定位父级offsetParent.本文将详细介绍该部分内容 定位父级 在理解偏移大小之前,首先要理解offsetParent.人们并没有把offsetParent翻译为偏移父级,而是翻译成定位父级

height clientHeight scrollHeight offsetHeight的大致区别

这主要是针对火狐浏览器来讲的: height:就是div的高度: clientHeight:一般情况下和height是一样的,如果div有滚动条,那么clientHeight = height - 滚动条的高度: var w= document.documentElement.clientWidth || document.body.clientWidth; var h= document.documentElement.clientHeight || document.body.clientH

clientHeight , scrollHeight , offsetHeight之间的区别及兼容方案

clientHeight , scrollHeight , offsetHeight相信每个人都用过,可是每次用都要查一下到底哪个是文档大小哪个是视口大小,还有头疼的兼容问题. 先来官方的了解一下这三个属性: clientHeight:元素客户区的大小,指的是元素内容及其边框所占据的空间大小(经过实践取出来的大多是视口大小) scrollHeight: 滚动大小,指的是包含滚动内容的元素大小(元素内容的总高度) offsetHeight: 偏移量,包含元素在屏幕上所用的所有可见空间(包括所有的内