How to detect page zoom level in all modern browsers?

后端 未结 28 2237
慢半拍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条回答
  •  梦毁少年i
    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);
    

提交回复
热议问题