window.screen.width和window.screen.height无法在iPad 3上运行 [英] window.screen.width and window.screen.height not working on iPad 3

查看:125
本文介绍了window.screen.width和window.screen.height无法在iPad 3上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这些功能

window.screen.width
window.screen.height

检测用户的屏幕分辨率。
它们就像PC上的魅力一样,但它们不适用于iPad 3.
它们输出768和1024而不是2048和1536.

to detect the screen resolution of the user. They work like a charm on PCs, but they don't on iPad 3. They output 768 and 1024 instead of 2048 and 1536.

有人可以帮助我吗?
提前谢谢

Can somebody help me, please? Thank you in advance

推荐答案

是的。欢迎来到有趣的移动设备世界!

Yep. Welcome to the interesting world of Mobile devices!

iPad 3(和其他视网膜设备)使用 window.devicePixelRatio 设置为 2 以显示它们与逻辑像素具有不同的css像素。 iPad 3仍然报告1024次; 768,因为这是CSS像素的数量。

The iPad 3 (and other retina devices) use window.devicePixelRatio set to 2 to show that they have different css pixels to logical pixels. An iPad 3 still reports 1024 × 768, as that is the number of CSS pixels.

作为另一个混乱的来源,一些Android设备报告视口宽度,一些物理宽度,这意味着,如果你问一些Android设备,如果文档很长, window.screen.height 将是数千和数千。

As another source of confusion, some Android devices report the viewport width, and some the physical width, meaning that if you ask some Android devices, the window.screen.height will be thousands and thousands if the document is long.

简而言之,对于您的问题,请使用 window.devicePixelRatio 作为乘数。我会使用类似

In short, for your problem, use window.devicePixelRatio as a multiplier. I'd use something like

if(!window.devicePixelRatio) {
    window.devicePixelRatio = 1;
}

确保如果未设置它在您之前声明为1开始。

To ensure that if it isn't set that it's declared as 1 before you start.

这篇关于window.screen.width和window.screen.height无法在iPad 3上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆