然后一试,果然就可以了,看来3C对于代码的规范影响还是蛮大的。
- window.innerHeight/Width
- 很多浏览器都支持,但是IE却不支持!
- document.body.clientHeight/Width
- 大部分浏览器(包括IE)都支持!
- document.documentElement.璫lientHeight/Width
- 很多支持DOM的浏览器都支持,包括IE!
clientWidth/Height属性显得有些混乱,因为对于不同的浏览器,它代表着不同的意思,而且更为可怕的是,即使对于同一个浏览器,如果其对于文档类型的声明不同(比如strict模式或者quirks模式),它所代表的含义也不尽相同。有时候它门表示window窗口大小,有时候则表示document的大小。下面的表格表明了它们在不同浏览器和不同模式下的不同含义:
| Opera 7+ | window | window | document |
|---|---|---|---|
| Mozilla | window | window | document |
| KHTML | window | window | document |
| iCab | window | window | N/A |
| Opera 6 | window | window | N/A |
| IE 4 | N/A | window | N/A |
| IE 5-6 quirks | N/A | window | 0 |
| IE 6 strict | N/A | document | window |
| ICEbrowser | window | window | document |
| Netscape 4 | window | N/A | N/A |
下面就是获取窗口大小的函数:
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
window.alert( 'Width = ' + myWidth );
window.alert( 'Height = ' + myHeight );
}
另外,该文还介绍了一个有关浏览器滚动条的属性值在不同浏览器下的区别。
当咱们要做一些特效,比如雪花下落,或者让某一个菜单保持在同一个位置而不碎滚动条变化时,就需要获取浏览器滚动条当前相对于屏幕的位置。
其函数如下:
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
//Netscape compliant
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
//DOM compliant
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
//IE6 standards compliant mode
scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return [ scrOfX, scrOfY ];
}
浙公网安备 33010602011771号