window.screen.width和window.screen.height在iPad3上不起作用
window.screen.width and window.screen.height not working on iPad 3
我正在使用函数
window.screen.width
window.screen.height
以检测用户的屏幕分辨率。它们在个人电脑上很有魅力,但在iPad3上却没有。它们输出768和1024,而不是2048和1536。
有人能帮帮我吗?提前感谢
是的。欢迎来到移动设备的有趣世界!
iPad3(和其他视网膜设备)使用设置为2
的window.devicePixelRatio
来显示它们具有不同的css像素和逻辑像素。iPad3仍然报告1024×768,因为这是CSS像素的数量。
另一个令人困惑的来源是,一些Android设备报告了视口宽度,而另一些报告了物理宽度,这意味着如果你问一些Android设备,如果文档很长,window.screen.height
将是成千上万。
简而言之,对于您的问题,使用window.devicePixelRatio
作为乘数。我会用之类的东西
if(!window.devicePixelRatio) {
window.devicePixelRatio = 1;
}
确保如果未设置,则在开始之前将其声明为1。
if(window.devicePixelRatio !== undefined) {
dpr = window.devicePixelRatio;
} else {
dpr = 1;
}
var screen_width = window.screen.width * dpr;
var screen_height = window.screen.height * dpr;
这个解决方案非常有效。
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- 直接下载文件,而不是从window.open(url)
- $window.ga在AngularJS事件中未定义
- 如何将PDF作为二进制文件传递到window.open()
- window.onload没有'无法在Android WebView中工作
- window.location替换并传递URL历史记录条目中的变量
- window.on.scroll事件未启动
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- 为什么window.open不打开一个以变量形式给出的链接
- window.location使用jquery mobile实现chrome跳转
- window.opener.document在ie中不起作用
- 如何在PhantomJS中更改window.screen尺寸
- window.screen.width和window.screen.height在iPad3上不起作用
- window.screen in FireFox
- 当屏幕 CSS 像素比大于 1 时,在不同的浏览器上获取相同的 window.screen.height 和 windo
- Android浏览器的screen.width,screen.height和window.innerWidth和wind
- Internet Explorer 中的 Window.screen 和多个监视器
- window.screen.width在阶梯模式下提供iPhone/iPad屏幕的高度
- 使用window.screen安全吗?
- 当屏幕方向改变时,window.screen.width值在iPad浏览器中不改变