眾所周知,javascript差不多由三部分組成,ECMAscript,BOM,DOM。而BOM代表的是游覽器模型,各大游覽器兼容是最麻煩的,今天主要是幾個游覽器對窗口,視口位置、大小的兼容處理。
1、游覽器視口距離屏幕的位置(左上角)left及top
ie,chrome : 支持window.screenLeft,window.screenTop? --------------ie/55,0( 默認加上上面導(dǎo)航欄的高度) chrome/0,0
firefog,chrome : window.screenX,window.screenY ---------------------------firefog/-8,-8 ? ? ? chrome /0,0
兼容:var left = typeof? window.screenLeft === "number" ? window.screenLeft : window.screenX; var top = typeof window.screenTop === "number" ? window.screenTop : window.screenY;
2、window.innerWidth、document.documentElement.clientWidth、document.body.clientWidth
var width,height;
if( window.innerWidth ){ ? ? ? ? ? ? ? ? ? ? ?? // firefog? 和? chrome 本身也支持document.documentElement.clientWidth,直接window屬性提高點點點性能
width = window.innerWidth; ? ? ? ? ? ? ? ? ? ? ?
height = window.innerHeight;
}else{
? ? if( document.compatMode === "CSS1Compat" ){ ? ? ? ? ? ? //兼容ie6的怪異模式BackCompat
? ? ? ? ? ?? width = document.documentElement.clientWidth;
? ? ? ? ? ?? height = document.documentElement.clientHeight;
? ? }else {
? ? ? ? ? ?? width = document.body.clientWidth;
? ? ? ? ? ?? height = document.body.clientHeight;
?? }
}
3、