[英]Finding screen width of HD device with JavaScript
I need to find the screen width of a device with JavaScript, I am unable to change the html and do not want to inject the meta viewport tag before doing so. 我需要使用JavaScript找到设备的屏幕宽度,我无法更改html并且不想在执行此操作之前注入元视口标记。 Essentially I need to detect whether the device width is below 600px (in CSS pixels). 基本上我需要检测设备宽度是否低于600px(以CSS像素为单位)。
I have seen various recommendations but unfortunately I am getting different behaviour between iOS Safari and the default Android browser: 我已经看到了各种 推荐,但遗憾的是我在iOS Safari和默认Android浏览器之间遇到了不同的行为:
Command iPhone4 GalaxyS3
------------------------------------------------------------------------
window.devicePixelRatio 2 2
window.innerWidth 982 626
window.outerWidth 326 720
screen.width 320 720
screen.availWidth 320 720
matchMedia('(max-width: 599px)').matches false false
matchMedia('(max-device-width: 599px)').matches true false
Unfortunately the inconsistencies of the above values means I don't seem to be able to do it with JavaScript (eg I could divide screen.width by window.devicePixelRatio on Android, but this would then be incorrect on iOS). 不幸的是,上述值的不一致意味着我似乎无法使用JavaScript(例如,我可以通过Android上的window.devicePixelRatio来划分screen.width,但这在iOS上是不正确的)。
Is there a reliable, cross browser way of doing this? 有可靠的,跨浏览器的方式吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.