How can you get the CSS pixel / device pixel ratio?

前端 未结 4 1855
臣服心动
臣服心动 2020-12-04 16:36

I want to find the ratio between CSS pixels and device pixels.

Edit: I should have realized that this is just zoom level. I\'ve added an answer to t

4条回答
  •  情书的邮戳
    2020-12-04 17:22

    You can always use the following method and it would work in all the browsers

    window.getDevicePixelRatio = function () {
        var ratio = 1;
        // To account for zoom, change to use deviceXDPI instead of systemXDPI
        if (window.screen.systemXDPI !== undefined && window.screen.logicalXDPI       !== undefined && window.screen.systemXDPI > window.screen.logicalXDPI) {
            // Only allow for values > 1
            ratio = window.screen.systemXDPI / window.screen.logicalXDPI;
        }
        else if (window.devicePixelRatio !== undefined) {
            ratio = window.devicePixelRatio;
        }
        return ratio;
    };
    

提交回复
热议问题