javascript - 如何在 iOS Safari 中获取当前屏幕方向?
问题描述
我正在尝试获取 iOS 的当前屏幕方向,但是我的功能适用于 chrome 开发工具模拟器和桌面,但不适用于 iOS。
这是我的功能:
export type ScreenOrientation = "landscape" | "portrait";
export function getScreenOrientation(): ScreenOrientation
{
if (window.screen.availHeight > window.screen.availWidth)
return "portrait";
else
return "landscape";
}
以下是我的程序如何粗略地检测屏幕方向变化并使用该功能:
import { getScreenOrientation } from "../../Utils/getOrientation";
const shoWPortraitModeError = getScreenOrientation() == "landscape" ? false : true;
window.onorientationchange = function () {
const newState = getScreenOrientation() == "landscape" ? false : true;
console.log("window.onorientationchange: ", newState)
shoWPortraitModeError = newState;
};
我尝试使用window.screen.height
,window.screen.width
但没有奏效。这是功能:
export type ScreenOrientation = "landscape" | "portrait";
export function getScreenOrientation(): ScreenOrientation
{
if (window.screen.availHeight > window.screen.availWidth)
return "portrait";
else
return "landscape";
}
我在 mac vm 上启动 iOS safari 调试器,我注意到window.screen
当我转动屏幕时该值没有改变:
这让我想知道我可以使用哪些不同的属性来检测 ios 上的屏幕方向?
解决方案
在 safari 调试器的开发控制台的智能感知中挖掘之后,我发现可以使用window.innerHeight
和window.innerWidth
属性来检测屏幕方向。
以下是如何使用该功能:
export type ScreenOrientation = "landscape" | "portrait";
export function getScreenOrientation(): ScreenOrientation
{
if (window.innerHeight > window.innerWidth)
return "portrait";
else
return "landscape";
}
推荐阅读
- java - 如何使用我的 java 应用程序控制 nginx 反向代理?
- algorithm - 将 n 个对象分成 k 个组的方法有多少种,使得没有一个组的对象比以前形成的组少?
- javascript - 删除firebase上的节点时出现问题
- aspose - 打开未使用密码加密的文档
- javascript - 我怎样才能打破表格的段落属性
- java - Hybris SOLR - 使用自由范围滑块的 Facet priceValue
- python - AttributeError:“str”对象没有属性“firstChild”
- javascript - 从 uniq id 中收集所有值并传递它们
- regex - Match regex until the first occurrence of special character '->'
- r - 如何在 r 中找到平均绝对偏差 (MAD)