javascript - 安卓浏览器 : get scale factor

标签 javascript android browser scaling

是否有可能以某种方式在 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

关于javascript - 安卓浏览器 : get scale factor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4448583/

相关文章:

browser - PC 上同一浏览器的多个版本

javascript - 纯 JavaScript 中的单击事件

javascript - 在不使用 jquery 的情况下在 div/span 元素溢出上添加点/省略号

javascript - Highcharts + AngularJS 上的随机数

javascript - 在 main.js 和附加到选项卡的脚本之间正确使用 port.emit 和 port.on

Android - proguard 忽略 - 保持 Swig 方法

Android:为什么在创建选项卡时调用 onTabSelected?

javascript - 如何使用 javascript 在单击按钮时将浏览器窗口从背景返回到前面?

java - 选项卡式 Activity 中未显示工具栏标题

javascript - 我可以强制浏览器在继续执行 javascript 之前呈现出 DOM 更改吗?