【发布时间】:2021-06-24 10:36:01
【问题描述】:
我正在尝试获取 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 上的屏幕方向?
【问题讨论】:
标签: javascript ios typescript mobile-safari