zoukankan      html  css  js  c++  java
  • 花点时间搞清top、clientTop、scrollTop、offsetTop

    【转贴】花点时间搞清top、clientTop、scrollTop、offsetTop

     

    scrollHeight: 获取对象的滚动高度。 
    scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
    scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
    scrollWidth:获取对象的滚动宽度

    offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度
    offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置
    offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置
    offsetWidth:是对象的可见宽度,包滚动条等边线,会随窗口的显示大小改变
    event.clientX 相对文档的水平座标
    event.clientY 相对文档的垂直座标

    clientWidth:是对象可见的宽度,不包滚动条等边线,会随窗口的显示大小改变。
    clientHeight:都认为是内容可视区域的高度,也就是说页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。

    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均无关)

    测试:

    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style type="text/css">
    </style>
    </head>
    <body>
    <SCRIPT LANGUAGE="JavaScript">
    var s = "";
    s += " 网页可见区域宽:"+ document.body.clientWidth;
    s += " 网页可见区域高:"+ document.body.clientHeight;
    s += " 网页可见区域宽:"+ document.body.offsetWidth +" (包括边线的宽)";
    s += " 网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
    s += " 网页正文全文宽:"+ document.body.scrollWidth;
    s += " 网页正文全文高:"+ document.body.scrollHeight;
    s += " 网页被卷去的高:"+ document.body.scrollTop;
    s += " 网页被卷去的左:"+ document.body.scrollLeft;
    s += " 网页正文部分上:"+ window.screenTop;
    s += " 网页正文部分左:"+ window.screenLeft;
    s += " 屏幕分辨率的高:"+ window.screen.height;
    s += " 屏幕分辨率的宽:"+ window.screen.width;
    s += " 屏幕可用工作区高度:"+ window.screen.availHeight;
    s += " 屏幕可用工作区宽度:"+ window.screen.availWidth;
    alert(s);
    </SCRIPT>
    </body>
    </html>

    以上内容来自 http://www.it596.com/(程序员的技术讨论与分享园地)

    来自: http://hi.baidu.com/niegerdai/blog/item/f47227517d95c8858c5430ba.html

  • 相关阅读:
    使用AWS、Docker与Rancher提供弹性的生产级服务
    如何用微服务重构应用程序
    Prometheus入门
    Docker监控:最佳实践以及cAdvisor和Prometheus监控工具的对比
    前后端分离session不一致问题
    Eclipse整合SSM框架+AOP+全局异常处理
    SSM配置基于注解AOP
    有关Linux
    Tomcat下配置javaWeb访问路径-Linux
    Linux下如何安装Nginx
  • 原文地址:https://www.cnblogs.com/jquery37/p/4633604.html
Copyright © 2011-2022 走看看