How to detect page zoom level in all modern browsers?

后端 未结 28 1762
慢半拍i
慢半拍i 2020-11-21 05:27
  1. How can I detect the page zoom level in all modern browsers? While this thread tells how to do it in IE7 and IE8, I can\'t find a good cross-browser solution.

相关标签:
28条回答
  • 2020-11-21 05:51

    This has worked great for me in webkit-based browsers (Chrome, Safari):

    function isZoomed() {
        var width, mediaQuery;
    
        width = document.body.clientWidth;
        mediaQuery = '(max-width: ' + width + 'px) and (min-width: ' + width + 'px)';
    
        return !window.matchMedia(mediaQuery).matches;
    }
    

    Doesn't seem to work in Firefox though.

    This also works in WebKit:

    var zoomLevel = document.width / document.body.clientWidth;
    
    0 讨论(0)
  • 2020-11-21 05:52

    try this

    alert(Math.round(window.devicePixelRatio * 100));
    
    0 讨论(0)
  • 2020-11-21 05:53

    My coworker and I used the script from https://github.com/tombigel/detect-zoom. In addition, we also dynamically created a svg element and check its currentScale property. It works great on Chrome and likely most browsers too. On FF the "zoom text only" feature has to be turned off though. SVG is supported on most browsers. At the time of this writing, tested on IE10, FF19 and Chrome28.

    var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
    svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
    svg.setAttribute('version', '1.1');
    document.body.appendChild(svg);
    var z = svg.currentScale;
    ... more code ...
    document.body.removeChild(svg);
    
    0 讨论(0)
  • 2020-11-21 05:53

    In Internet Explorer 7, 8 & 9, this works:

    function getZoom() {
        var screen;
    
        screen = document.frames.screen;
        return ((screen.deviceXDPI / screen.systemXDPI) * 100 + 0.9).toFixed();
    }
    

    The "+0.9" is added to prevent rounding errors (otherwise, you would get 104% and 109% when the browser zoom is set to 105% and 110% respectively).

    In IE6 zoom doesn't exists, so it is unnecessary to check the zoom.

    0 讨论(0)
  • 2020-11-21 05:54
    function supportFullCss3()
    {
        var div = document.createElement("div");
        div.style.display = 'flex';
        var s1 = div.style.display == 'flex';
        var s2 = 'perspective' in div.style;
    
        return (s1 && s2);
    };
    
    function getZoomLevel()
    {
        var screenPixelRatio = 0, zoomLevel = 0;
    
        if(window.devicePixelRatio && supportFullCss3())
            screenPixelRatio = window.devicePixelRatio;
        else if(window.screenX == '0')
            screenPixelRatio = (window.outerWidth - 8) / window.innerWidth;
        else
        {
            var scr = window.frames.screen;
            screenPixelRatio = scr.deviceXDPI / scr.systemXDPI;
        }
    
        //---------------------------------------
        if (screenPixelRatio <= .11){ //screenPixelRatio >= .01 &&
          zoomLevel = "-7";
        } else if (screenPixelRatio <= .25) {
          zoomLevel = "-6";
        }else if (screenPixelRatio <= .33) {
          zoomLevel = "-5.5";
        } else if (screenPixelRatio <= .40) {
          zoomLevel = "-5";
        } else if (screenPixelRatio <= .50) {
          zoomLevel = "-4";
        } else if (screenPixelRatio <= .67) {
          zoomLevel = "-3";
        } else if (screenPixelRatio <= .75) {
          zoomLevel = "-2";
        } else if (screenPixelRatio <= .85) {
          zoomLevel = "-1.5";
        } else if (screenPixelRatio <= .98) {
          zoomLevel = "-1";
        } else if (screenPixelRatio <= 1.03) {
          zoomLevel = "0";
        } else if (screenPixelRatio <= 1.12) {
          zoomLevel = "1";
        } else if (screenPixelRatio <= 1.2) {
          zoomLevel = "1.5";
        } else if (screenPixelRatio <= 1.3) {
          zoomLevel = "2";
        } else if (screenPixelRatio <= 1.4) {
          zoomLevel = "2.5";
        } else if (screenPixelRatio <= 1.5) {
          zoomLevel = "3";
        } else if (screenPixelRatio <= 1.6) {
          zoomLevel = "3.3";
        } else if (screenPixelRatio <= 1.7) {
          zoomLevel = "3.7";
        } else if (screenPixelRatio <= 1.8) {
          zoomLevel = "4";
        } else if (screenPixelRatio <= 1.9) {
          zoomLevel = "4.5";
        } else if (screenPixelRatio <= 2) {
          zoomLevel = "5";
        } else if (screenPixelRatio <= 2.1) {
          zoomLevel = "5.2";
        } else if (screenPixelRatio <= 2.2) {
          zoomLevel = "5.4";
        } else if (screenPixelRatio <= 2.3) {
          zoomLevel = "5.6";
        } else if (screenPixelRatio <= 2.4) {
          zoomLevel = "5.8";
        } else if (screenPixelRatio <= 2.5) {
          zoomLevel = "6";
        } else if (screenPixelRatio <= 2.6) {
          zoomLevel = "6.2";
        } else if (screenPixelRatio <= 2.7) {
          zoomLevel = "6.4";
        } else if (screenPixelRatio <= 2.8) {
          zoomLevel = "6.6";
        } else if (screenPixelRatio <= 2.9) {
          zoomLevel = "6.8";
        } else if (screenPixelRatio <= 3) {
          zoomLevel = "7";
        } else if (screenPixelRatio <= 3.1) {
          zoomLevel = "7.1";
        } else if (screenPixelRatio <= 3.2) {
          zoomLevel = "7.2";
        } else if (screenPixelRatio <= 3.3) {
          zoomLevel = "7.3";
        } else if (screenPixelRatio <= 3.4) {
          zoomLevel = "7.4";
        } else if (screenPixelRatio <= 3.5) {
          zoomLevel = "7.5";
        } else if (screenPixelRatio <= 3.6) {
          zoomLevel = "7.6";
        } else if (screenPixelRatio <= 3.7) {
          zoomLevel = "7.7";
        } else if (screenPixelRatio <= 3.8) {
          zoomLevel = "7.8";
        } else if (screenPixelRatio <= 3.9) {
          zoomLevel = "7.9";
        } else if (screenPixelRatio <= 4) {
          zoomLevel = "8";
        } else if (screenPixelRatio <= 4.1) {
          zoomLevel = "8.1";
        } else if (screenPixelRatio <= 4.2) {
          zoomLevel = "8.2";
        } else if (screenPixelRatio <= 4.3) {
          zoomLevel = "8.3";
        } else if (screenPixelRatio <= 4.4) {
          zoomLevel = "8.4";
        } else if (screenPixelRatio <= 4.5) {
          zoomLevel = "8.5";
        } else if (screenPixelRatio <= 4.6) {
          zoomLevel = "8.6";
        } else if (screenPixelRatio <= 4.7) {
          zoomLevel = "8.7";
        } else if (screenPixelRatio <= 4.8) {
          zoomLevel = "8.8";
        } else if (screenPixelRatio <= 4.9) {
          zoomLevel = "8.9";
        } else if (screenPixelRatio <= 5) {
          zoomLevel = "9";
        }else {
          zoomLevel = "unknown";
        }
    
        return zoomLevel;
    };
    
    0 讨论(0)
  • 2020-11-21 05:56

    The problem lies in the types of monitor used, a 4k monitor vs standard monitor. Chrome is by far the smartest at being able to tell us what the zoom level is just by using window.devicePixelRatio, apparently it can tell what the pixel density is and reports back the same number to matter what.

    Other browsers, not so much. IE and Edge are probably the worst, as they handle zoom levels much differently. To get the same size text on a 4k monitor, you have to select 200%, instead of 100% on a standard monitor.

    As of May 2018, here's what I have to detect zoom levels for a few of the most popular browsers, Chrome, Firefox, and IE11. I have it tell me what the zoom percentage is. For IE, it reports 100% even for 4k monitors that are actually at 200%, but the text size is really the same.

    Here's a fiddle: https://jsfiddle.net/ae1hdogr/

    If anyone would care to take a stab at other browsers and update the fiddle, then please do so. My main goal was to get these 3 browsers covered to detect if people were using a zoom factor greater than 100% to use my web application and display a notice suggesting a lessor zoom factor.

    0 讨论(0)
提交回复
热议问题