How to detect page zoom level in all modern browsers?

后端 未结 28 1831
慢半拍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 06:13

    A workaround for FireFox 16+ to find DPPX (zoom level) purely with JavaScript:

    var dppx = (function (precision) {
      var searchDPPX = function(level, min, divisor) {
        var wmq = window.matchMedia;
        while (level >= min && !wmq("(min-resolution: " + (level/divisor) + "dppx)").matches) {
          level--;
        }
        return level;
      };
    
      var maxDPPX = 5.0; // Firefox 22 has 3.0 as maximum, but testing a bit greater values does not cost much
      var minDPPX = 0.1; // Firefox 22 has 0.3 as minimum, but testing a bit smaller values does not cost anything
      var divisor = 1;
      var result;
      for (var i = 0; i < precision; i++) {
        result = 10 * searchDPPX (maxDPPX, minDPPX, divisor);
        maxDPPX = result + 9;
        minDPPX = result;
        divisor *= 10;
      }
    
      return result / divisor;
    }) (5);
    
    0 讨论(0)
  • 2020-11-21 06:15

    For me, for Chrome/Webkit, document.width / jQuery(document).width() did not work. When I made my window small and zoomed into my site such that horizontal scrollbars appeared, document.width / jQuery(document).width() did not equal 1 at the default zoom. This is because document.width includes part of the document outside the viewport.

    Using window.innerWidth and window.outerWidth worked. For some reason in Chrome, outerWidth is measured in screen pixels and innerWidth is measured in css pixels.

    var screenCssPixelRatio = (window.outerWidth - 8) / window.innerWidth;
    if (screenCssPixelRatio >= .46 && screenCssPixelRatio <= .54) {
      zoomLevel = "-4";
    } else if (screenCssPixelRatio <= .64) {
      zoomLevel = "-3";
    } else if (screenCssPixelRatio <= .76) {
      zoomLevel = "-2";
    } else if (screenCssPixelRatio <= .92) {
      zoomLevel = "-1";
    } else if (screenCssPixelRatio <= 1.10) {
      zoomLevel = "0";
    } else if (screenCssPixelRatio <= 1.32) {
      zoomLevel = "1";
    } else if (screenCssPixelRatio <= 1.58) {
      zoomLevel = "2";
    } else if (screenCssPixelRatio <= 1.90) {
      zoomLevel = "3";
    } else if (screenCssPixelRatio <= 2.28) {
      zoomLevel = "4";
    } else if (screenCssPixelRatio <= 2.70) {
      zoomLevel = "5";
    } else {
      zoomLevel = "unknown";
    }
    
    0 讨论(0)
  • 2020-11-21 06:16

    On Chrome

    var ratio = (screen.availWidth / document.documentElement.clientWidth);
    var zoomLevel = Number(ratio.toFixed(1).replace(".", "") + "0");
    
    0 讨论(0)
  • 2020-11-21 06:16

    Have it currently working however still need to separate by browser. Tested successfully on Chrome(75) and Safari(11.1) (haven't found way for FF as of yet). It also gets the zoom value correct on retina display and calculations are triggered on resize event.

        private pixelRatio() {
          const styleString = "(min-resolution: 2dppx), (-webkit-min-device-pixel-ratio: 1.5),(-moz-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5)";
          const chromeRatio = (Math.round((this.window.outerWidth / this.window.innerWidth)*100) / 100);
          const otherRatio = (Math.round(window.devicePixelRatio * 100) / 100);
          const resizeValue = (this.isChrome()) ? chromeRatio : otherRatio;
    
          return resizeValue || (this.window.matchMedia && this.window.matchMedia(styleString).matches ? 2 : 1) || 1;
        }
    
    
      private isChrome():boolean {
        return (!!this.window.chrome && !(!!this.window.opera || this.window.navigator.userAgent.indexOf(' Opera') >= 0))
      }
    
      private chrome() {
        const zoomChrome = Math.round(((this.window.outerWidth) / this.window.innerWidth)*100) / 100;
        return {
          zoom: zoomChrome,
          devicePxPerCssPx: zoomChrome1 * this.pixelRatio()
        };
      }
    
    0 讨论(0)
提交回复
热议问题