js下的window.screenLeft,window.screenX,window.innerWidth等

眾所周知,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、

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容