2009-03-25 12 views
13

En Internet Explorer 7 algunas propiedades (coordenadas del mouse) se tratan como físicas mientras que otras son lógicas (desplazamiento). Esto esencialmente requiere que los desarrolladores web tengan en cuenta o calculen el estado del zoom. En la versión IE8, todas las propiedades son lógicas.¿Cómo obtener nivel de zoom en Internet Explorer 7? (javascript)

+0

Véase también http://stackoverflow.com/questions/1713771/how-to-detect-page-zoom-level-in-all-modern-browsers – ripper234

Respuesta

10

Se puede conseguir usando:

var b = document.body.getBoundingClientRect();  
alert((b.right - b.left)/document.body.clientWidth); 

Muchas gracias @niclasnorgren!

8

Además, si necesita comprobar en IE 8, puede usar window.screen.deviceXDPI y window.screen.deviceYDPI. El valor predeterminado es 96 ppp, y si se amplía, el número será mayor (también conocido como 144 cuando se amplía 150%)

+0

gracias, eso era justo lo que necesitaba para encontrar la "relación de zoom" :-) – naivists

+2

window.screen.deviceXDPI funciona de manera confiable en los modos de emulación de IE9 para IE8, IE7. El método getBoundingClientRect() devuelve 100% en todos los modos de zoom. – ddotsenko

+0

Tenga en cuenta que screen.deviceXDPI es solo IE, no en Chrome. –

4

Hay un pequeño error de sintaxis (cuerpo en lugar de document.body) en la respuesta aceptada. Esto parece hacer el truco también.

var rect = document.body.getBoundingClientRect(); 
var zoomLevel = Math.round((rect.right-rect.left)/document.body.clientWidth * 100); 
0

he publicado solución para esto en otro post que usted puede conseguir esto aquí. que funcionará en IE7 también.

Auto-detect a screen resolution and change browser zoom with Javascript?

This will help to detect browser zoom tested on all browser 
<script> 
window.utility = function(utility){ 
utility.screen = { 
    rtime : new Date(1, 1, 2000, 12,00,00), 
    timeout : false, 
    delta : 200 
}; 
utility.getBrowser = function(){ 
    var $b = $.browser; 
    $.extend(utility.screen,$.browser); 
    utility.screen.isZoomed = false; 
    var screen = utility.screen; 
    screen.zoomf = screen.zoom = 1; 
    screen.width = window.screen.width; 
    screen.height = window.screen.height; 
    if($b.mozilla){ //FOR MOZILLA 
     screen.isZoomed = window.matchMedia('(max--moz-device-pixel-ratio:0.99), (min--moz-device-pixel-ratio:1.01)').matches; 
    } else { 
     if($b.chrome){ //FOR CHROME 
      screen.zoom = (window.outerWidth - 8)/window.innerWidth; 
      screen.isZoomed = (screen.zoom < .98 || screen.zoom > 1.02) 
     } else if($b.msie){//FOR IE7,IE8,IE9 
      var _screen = document.frames.screen; 
      screen.zoom = ((((_screen.deviceXDPI/_screen.systemXDPI) * 100 + 0.9).toFixed())/100); 
      screen.isZoomed = (screen.zoom < .98 || screen.zoom > 1.02); 
      if(screen.isZoomed) screen.zoomf = screen.zoom; 
      screen.width = window.screen.width*screen.zoomf; 
      screen.height = window.screen.height*screen.zoomf; 
     } 
    } 
    return utility.screen; 
}; 
    window.onresize = function(e){ 
     utility.screen.rtime = new Date(); 
     if (utility.screen.timeout === false) { 
       utility.screen.timeout = true; 
       setTimeout(window.resizeend, utility.screen.delta); 
     } 
    }; 
window.resizeend = function() { 
    if (new Date() - utility.screen.rtime < utility.screen.delta) { 
     setTimeout(window.resizeend, utility.screen.delta); 
    } else { 
     utility.screen.timeout = false; 
     utility.screen = utility.getBrowser(); 
     if(window.onresizeend) window.onresizeend (utility.screen); 
     if(utility.onResize) utility.onResize(utility.screen); 
    }    
}; 
window.onresizeend = function(screen){ 
    if(screen.isZoomed) 
     $('body').text('zoom is not 100%'); 
    else{ 
     $('body').text('zoom is 100% & browser resolution is'+[screen.width+'X'+screen.height]); 
    } 
}; 
$(document).ready(function(){ 
    window.onresize(); 
}); 
return utility; 
}({}); 
</script> 

Demo

Cuestiones relacionadas