JS文档模式 -document.compatMode

IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有很大差别,而在不声明Doctype的情况下,IE默认又是Quirks Mode。所以为兼容性考虑,我们可能需要获取当前的文档渲染方式。

document.compatMode正好派上用场,它有两种可能的返回值:BackCompat和CSS1Compat。

BackCompat:标准兼容模式关闭。浏览器客户区宽度是document.body.clientWidth;CSS1Compat:标准兼容模式开启。 浏览器客户区宽度是document.documentElement.clientWidth。

那么写了个准确获取网页客户区的宽高、滚动条宽高、滚动条Left和Top的代码:
view source
print?
01 if (document.compatMode == "BackCompat") {
02    cWidth = document.body.clientWidth;
03    cHeight = document.body.clientHeight;
04    sWidth = document.body.scrollWidth;
05    sHeight = document.body.scrollHeight;
06    sLeft = document.body.scrollLeft;
07    sTop = document.body.scrollTop;
08 }
09 else { //document.compatMode == "CSS1Compat"
10    cWidth = document.documentElement.clientWidth;
11    cHeight = document.documentElement.clientHeight;
12    sWidth = document.documentElement.scrollWidth;
13    sHeight = document.documentElement.scrollHeight;
14    sLeft = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft;
15    sTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop;
16 }

参考网址:http://hi.baidu.com/%C9%AE_%CC%C6/blog/item/a323a9df5a67c45d95ee3701.html

更多阅读:http://www.css88.com/archives/90,http://www.css88.com/archives/149

你可能感兴趣的:(html,浏览器,IE,Blog)