DIV CSS 佈局教程網

偵測瀏覽器縮放比例的 JS 程式碼
編輯:關於JavaScript     

以下整理在不同瀏覽器中偵測縮放比例的各種方式與實測結果。

Chrome

window.devicePixelRatio : 2 (準確)
screen.deviceXDPI / screen.logicalXDPI : NaN
window.outerWidth / window.innerWidth : 2
document.documentElement.offsetHeight / window.innerHeight : 0.0206... (有相關性)
window.top.outerWidth / window.top.innerWidth : 2

Firefox

window.devicePixelRatio : 1.5 (準確)
screen.deviceXDPI / screen.logicalXDPI : NaN
window.outerWidth / window.innerWidth : 1.0114...
document.documentElement.offsetHeight / window.innerHeight : 0.0233... (有相關性)
window.top.outerWidth / window.top.innerWidth : 1.0114...

IE8

window.devicePixelRatio : undefined
screen.deviceXDPI / screen.logicalXDPI : 1.5416... (大致準確)
window.outerWidth / window.innerWidth : NaN
document.documentElement.offsetHeight / window.innerHeight : NaN

IE11

window.devicePixelRatio : 1.5 (準確)
screen.deviceXDPI / screen.logicalXDPI : 1.5
window.outerWidth / window.innerWidth : 1.0084...
document.documentElement.offsetHeight / window.innerHeight : 0.0220... (有相關性)

IE10 / IE9

window.devicePixelRatio : undefined
screen.deviceXDPI / screen.logicalXDPI : 1.5 (準確)
window.outerWidth / window.innerWidth : 1.0084...
document.documentElement.offsetHeight / window.innerHeight : 1

360 瀏覽器 6.3(完全沒反應)

window.devicePixelRatio : undefined
screen.deviceXDPI / screen.logicalXDPI : 1
window.outerWidth / window.innerWidth : NaN

360 極速瀏覽器

window.devicePixelRatio : 1
screen.deviceXDPI / screen.logicalXDPI : NaN
window.outerWidth / window.innerWidth : 1.5
window.top.outerWidth / window.top.innerWidth : 1.5 (準確)

搜狗高速瀏覽器(完全沒反應)

window.devicePixelRatio : undefined
screen.deviceXDPI / screen.logicalXDPI : 1
window.outerWidth / window.innerWidth : NaN

淘寶瀏覽器

window.devicePixelRatio : 1
screen.deviceXDPI / screen.logicalXDPI : NaN
window.outerWidth / window.innerWidth : 2.0710... (大致準確)

綜合來看,window.devicePixelRatio 在支援它的瀏覽器(如 Chrome、Firefox、IE11)中最準確;舊版 IE 則可用 screen.deviceXDPI / screen.logicalXDPI 來近似推算縮放比例。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved