zoukankan      html  css  js  c++  java
  • 看图理解scrollTop,scrollLeft,clientWidth,clientHeight,offsetWidth,offsetHeight

    每次用JS获取页面的高宽时总都是相当的揪心,同一个属性在不同的浏览器或不同的W3C标准下所表示的意思都不尽相同。以下就针对页面的实际高宽和可见区域做个总结,以便大家查阅!


    1. W3C标准的情况下

    W3C标准页面,即在HTML代码头部加入

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

     

    IE中:
    document.body.clientWidth ==> BODY
    对象宽度
    document.body.clientHeight ==> BODY
    对象高度
    document.documentElement.clientWidth ==>
    可见区域宽度
    document.documentElement.clientHeight ==>
    可见区域高度

     

    FireFox中:
    document.body.clientWidth ==> BODY
    对象宽度
    document.body.clientHeight ==> BODY
    对象高度
    document.documentElement.clientWidth ==>
    可见区域宽度
    document.documentElement.clientHeight ==>
    可见区域高度

     

    Opera中:
    document.body.clientWidth ==>
    可见区域宽度
    document.body.clientHeight ==>
    可见区域高度
    document.documentElement.clientWidth ==>
    页面对象宽度(即BODY对象宽度加上Margin宽)
    document.documentElement.clientHeight ==>
    页面对象高度(即BODY对象高度加上Margin高)



    2. 在无W3C标准的情况下

    即在HTML代码头部无

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

     

    IE中:
    document.body.clientWidth ==>
    可见区域宽度
    document.body.clientHeight ==>
    可见区域高度
    document.documentElement.clientWidth ==> 0
    document.documentElement.clientHeight ==> 0

     

    FireFox中:
    document.body.clientWidth ==>
    可见区域宽度
    document.body.clientHeight ==>
    可见区域高度
    document.documentElement.clientWidth ==>
    页面对象宽度(即BODY对象宽度加上Margin宽)
    document.documentElement.clientHeight ==>
    页面对象高度(即BODY对象高度加上Margin高)

     

    Opera中:
    document.body.clientWidth ==>
    可见区域宽度
    document.body.clientHeight ==>
    可见区域高度
    document.documentElement.clientWidth ==>
    页面对象宽度(即BODY对象宽度加上Margin宽)
    document.documentElement.clientHeight ==>
    页面对象高度(即BODY对象高度加上Margin高)



    3. 附录 - 页面高宽的其他相关属性

    <script>

    var s = '';

    s += "\r\n
    网页可见区域宽:"+ document.body.clientWidth;
    s += "\r\n
    网页可见区域高:"+ document.body.clientHeight;
    s += "\r\n
    网页可见区域宽:"+ document.body.offsetWidth + " (包括边线和滚动条的宽)";
    s += "\r\n
    网页可见区域高:"+ document.body.offsetHeight + " (包括边线的宽)";
    s += "\r\n
    网页正文全文宽:"+ document.body.scrollWidth;
    s += "\r\n
    网页正文全文高:"+ document.body.scrollHeight;
    s += "\r\n
    网页被卷去的高(ff)"+ document.body.scrollTop;
    s += "\r\n
    网页被卷去的高(ie)"+ document.documentElement.scrollTop;
    s += "\r\n
    网页被卷去的左:"+ document.body.scrollLeft;
    s += "\r\n
    网页正文部分上:"+ window.screenTop;
    s += "\r\n
    网页正文部分左:"+ window.screenLeft;
    s += "\r\n
    屏幕分辨率的高:"+ window.screen.height;
    s += "\r\n
    屏幕分辨率的宽:"+ window.screen.width;
    s += "\r\n
    屏幕可用工作区高度:"+ window.screen.availHeight;
    s += "\r\n
    屏幕可用工作区宽度:"+ window.screen.availWidth;
    s += "\r\n
    你的屏幕设置是 "+ window.screen.colorDepth +" 位彩色";
    s += "\r\n
    你的屏幕设置 "+ window.screen.deviceXDPI +" 像素/英寸";

    alert(s);

    </script>

  • 相关阅读:
    C,LINUX,数据结构部分
    LINUX应用开发工程师职位(含答案)
    INT32 System_UserKeyFilter(NVTEVT evt, UINT32 paramNum, UINT32 *paramArray)
    屏幕调试
    1.ARM嵌入式体系结构与接口技术(Cortex-A8版)
    NT9666X调试log
    DemoKit编译过程错误
    selenium 代理设置
    pandas 轮询dataframe
    Spring 定时任务
  • 原文地址:https://www.cnblogs.com/xiaopin/p/1905009.html
Copyright © 2011-2022 走看看