编程语言
首页 > 编程语言> > javascript-Android浏览器:获取比例因子

javascript-Android浏览器:获取比例因子

作者:互联网

是否有可能以某种方式在Android浏览器上的javascript中获取当前的缩放比例?

解决方法:

The Android Browser and WebView support a DOM property that allows you to query the density of the current device—the window.devicePixelRatio DOM property. The value of this property specifies the scaling factor used for the current device. For example, if the value of window.devicePixelRatio is “1.0”, then the device is considered a medium density device and no scaling is applied by default; if the value is “1.5”, then the device is considered a high density device and the page is scaled 1.5x by default; if the value is “0.75”, then the device is considered a low density device and the page is scaled 0.75x by default. Of course, the scaling that the Android Browser and WebView apply is based on the web page’s target density—as described in the section about Defining the viewport target density, the default target is medium-density, but you can change the target to affect how your web page is scaled for different screen densities.

例如,以下是使用JavaScript查询设备密度的方法:

if (window.devicePixelRatio == 1.5) {
    alert("This is a high-density screen");
} else if (window.devicePixelRatio == 0.75) {
   alert("This is a low-density screen");
}

有关更多信息,您可以签出Tageting Screens from Web Apps

标签:scaling,browser,javascript,android
来源: https://codeday.me/bug/20191208/2094932.html