項目中,經常會碰到頁面被放大或者縮小,導致頁面顯示錯誤,js可以判斷頁面放大縮小。
// 若返回100則為默認無縮放,如果大於100則是放大,否則縮小
function detectZoom (){
var ratio = 0,
screen = window.screen,
ua = navigator.userAgent.toLowerCase();
if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
}
else if (~ua.indexOf('msie')) {
if (screen.deviceXDPI && screen.logicalXDPI) {
ratio = screen.deviceXDPI / screen.logicalXDPI;
}
}
else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
ratio = window.outerWidth / window.innerWidth;
}
if (ratio){
ratio = Math.round(ratio * 100);
}
return ratio;
};
window.onresize 事件可用於檢測頁面是否觸發了放大或縮小。
