gpt4 book ai didi

javascript - 媒体查询在 iphone 5 上失败

转载 作者:行者123 更新时间:2023-11-28 09:47:03 25 4
gpt4 key购买 nike

在模拟 iPhone5 的谷歌浏览器中,测试页面按预期工作:通过直接媒体查询、Javascript 媒体查询和简单的 javascript 检测到 width=568 和 height=320。然而,在真正的 iPhone5 中,直接媒体查询和 javascript 媒体查询失败,即使 javascript 正确检测到屏幕宽度和高度。我错过了什么?

  <html>
<head>
<meta name="viewport" content="width=device-width,
minimum-scale=1.0, maximum-scale=1.0" />
<style>
@media screen and (device-height:320px) and (device-width:568px) {
.deviceID:after {
content: ' ht=320 wid=568';
}
}
@media screen and (device-height:560px) and (device-width:320px) {
.deviceID:after {
content: ' ht=568 wid=320';
}
}
</style>
<body>
<script>
var mq;
mq = window.matchMedia( "(device-height:320px)" ); ht320=mq.matches;
mq = window.matchMedia( "(device-width:480px)" ); wid480=mq.matches;
mq = window.matchMedia( "(device-width:568px)" ); wid568=mq.matches;
mq = window.matchMedia( "(resolution:2.0dppx)" ); res20=mq.matches;
var res = window.devicePixelRatio?window.devicePixelRatio:1;
var or = window.orientation;
var aht = getHeight();
var awid = getWidth();
var ht = screen.height;
var wid = screen.width;
var or = getOrientation();

document.write( "<span class='deviceID'>Device: </span>" +
"<br>Ht320="+ht320 + " wid480="+wid480 + " wid568="+wid568 +
" res20="+res20 +
"<br>Size="+wid+"x"+ht + " avail="+aht+"x"+awid + " orien="+or +
" res=" + res
);

function getOrientation() {
var vor = window.orientation;
return (vor==undefined) ? "landscape" :
( Math.abs(window.orientation)-90 == 0 ? "landscape" : "portrait" );
}
function getWidth(){
return getOrientation() == "landscape" ? screen.availHeight : screen.availWidth;
}
function getHeight(){
return getOrientation() == "landscape" ? screen.availWidth : screen.availHeight;
}
</script>
</body>
</html>

最佳答案

您可能想从 QuirksMode 阅读这些幻灯片:

http://www.quirksmode.org/presentations/Spring2014/viewports_sf2.pdf

问题可能是媒体查询没有明确定义(因为 screen.width 没有明确定义)。

关于javascript - 媒体查询在 iphone 5 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25215428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com