简体   繁体   English

如何在Internet Explorer 7中获得缩放级别? (JavaScript的)

[英]How to get zoom level in Internet Explorer 7? (javascript)

In Internet Explorer 7 some properties (mouse coordinates) are treated as physical while others are logical (offset). 在Internet Explorer 7中,某些属性(鼠标坐标)被视为物理属性,而其他属性则是逻辑(偏移)。 This essentially required Web developers to be aware of or calculate the zoom state. 这基本上要求Web开发人员了解或计算缩放状态。 In IE8 release all properties are logical. 在IE8版本中,所有属性都是合乎逻辑的

You can get it using: 你可以使用它:

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

Thanks a lot @niclasnorgren ! 非常感谢@niclasnorgren

Also, if you need to do check in IE 8, you can use window.screen.deviceXDPI and window.screen.deviceYDPI. 此外,如果您需要在IE 8中进行检查,可以使用window.screen.deviceXDPI和window.screen.deviceYDPI。 The default is 96 dpi, and if you're zoomed, the number will be larger (aka 144 when zoomed 150%) 默认值为96 dpi,如果你进行了缩放,则数字会更大(在缩放150%时也称为144)

There is a small syntax error (body instead of document.body) on the accepted answer. 在接受的答案上有一个小的语法错误(正文而不是document.body)。 This seems to do the trick also. 这似乎也可以解决问题。

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

i have posted solution for this on another post you can get this here. 我已经在另一篇文章上发布了解决方案,你可以在这里得到它。 which will work in IE7 also. 这也适用于IE7。

Auto-detect a screen resolution and change browser zoom with Javascript? 使用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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM