【发布时间】:2020-07-04 20:13:22
【问题描述】:
我正在使用 JavaScript。我需要知道用户何时更改了手机的方向,并且我还需要知道哪个方向(0、90、-90 或 180)。
- 我尝试使用文档中的
orientationchange。
window.addEventListener("orientationchange", function(event) {
console.log("the orientation of the device is now " + event.target.screen.orientation.angle);
});
很遗憾,这在 IOS Safari 上不起作用。 event.target.screen 中没有 orientation 对象。
-
我尝试使用
window.orientation(似乎可以正常工作,但从文档中看,它已被弃用,必须避免使用。 -
我试过
resize监听器。
window.onresize = () => {
if(window.innerWidth >= window.innerHeight){
//landscape
}
else{
//portrait
}
};
这有两个问题。 1) 有时这在某些 iPhone 上或可能在同一部 iPhone 上无法正确检测到,但在不同的时间。 2) 我不知道如何知道角度。(0,90, -90, 180)。我不太了解屏幕和innerHeight 或innerWidth 或类似的东西。
任何适当的解决方案?
【问题讨论】:
标签: javascript ios safari