Ionic 应用程序中的屏幕尺寸错误

Del*_*Del 2 javascript android screen-resolution cordova ionic-framework

我有一个奇怪的问题。我正在开发一个 Ionic 应用程序,我需要将屏幕的宽度和高度发送到插件。我让他们在 javascript 方面。

我正在 Nexus 6 中进行测试,但值不正确。返回 660x412,当本设备的屏幕为 2k,2048x1080 时。

我试过:

screen.height
window.innerHeight
window.outerHeight
Run Code Online (Sandbox Code Playgroud)

而这一切都是错误的。

我的元视口是:

    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
Run Code Online (Sandbox Code Playgroud)

有人遇到过这个问题吗?

Ste*_*edy 5

问题是,这些宽度和高度可能并不代表物理屏幕,而是称为 CSS 像素的概念。

我们可以使用此值来获取物理屏幕大小,如下所示。

var physicalScreenWidth = window.screen.width * window.devicePixelRatio;
var physicalScreenHeight = window.screen.height * window.devicePixelRatio;
Run Code Online (Sandbox Code Playgroud)

请注意,该值会根据屏幕的当前方向而变化。