zoukankan      html  css  js  c++  java
  • document.compatMode属性

    document.compatMode用来判断当前浏览器采用的渲染方式。

    document.compatMode属性值:

    BackCompat:标准兼容模式关闭。
    CSS1Compat:标准兼容模式开启。

    当document.compatMode等于BackCompat时,浏览器客户区宽度是document.body.clientWidth;
    当document.compatMode等于CSS1Compat时,浏览器客户区宽度是document.documentElement.clientWidth。

    浏览器客户区高度(clientHeight)、滚动条高度(scrollHeight)、滚动条的Left(scrollLeft)、滚动条的Top(scrollTop)等等都是上面的情况。

    下面是demo:准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top的代码:

    if (document.compatMode == "BackCompat") {
    cWidth = document.body.clientWidth;
    cHeight = document.body.clientHeight;//浏览器客户区高度
    sWidth = document.body.scrollWidth;
    sHeight = document.body.scrollHeight;//滚动条高度
    sLeft = document.body.scrollLeft;//滚动条的Left
    sTop = document.body.scrollTop;//滚动条的Top
    }
    else { //document.compatMode == "CSS1Compat"
    cWidth = document.documentElement.clientWidth;
    cHeight = document.documentElement.clientHeight;
    sWidth = document.documentElement.scrollWidth;
    sHeight = document.documentElement.scrollHeight;
    sLeft = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft;
    sTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop;
    }

     

    简单点来说:
    var pageWidth=window.innerWidth,
    pageHeight=window.innerHeight;
    if(typeof pageWidth!='number'){
        if(document.compatMode=='CSS1Compat'){
        pageWidth=document.documentElement.clientWidth;
        pageHeight=document.documentElement.clientHeight;
        }
        else{
        pageWidth=document.body.clientWidth;
        pageHeight=document.body.clientHeight;
        }

    }
    (以上代码兼容目前流行的全部浏览器,包括:IE、Firefox、Safari、Opera、Chrome)

  • 相关阅读:
    List 与 Array 的相互转化及 List、Array、Set转为 String
    Java 序列化介绍及 Redis 序列化方式
    SpringBoot 整合 redis 实现 token 验证
    SpringBoot 整合 Redis 使用
    Map 某 value 为 对象数组,转为 ArrayList 对象集合
    JWT 基本使用
    Spring session + redis 实现 session共享入门
    HttpServletRequest + Filter 添加 header
    Git ahead(超前) 又behind(落后)
    web应用中路径跳转问题-相对路径、绝对路径
  • 原文地址:https://www.cnblogs.com/wuxn/p/4874536.html
Copyright © 2011-2022 走看看