我正在为Android和iPhone编写一个React Web App。我试图获得设备中方向变化的更新。在ChromeFirefox或大多数其他浏览器中,我可以做到。
window.addEventListener('orientationchange', this.orientationChange);
orientationChange()
{
console.log('info-view: orientation change detected ' + window.screen.orientation.type);
try {
screenOrientation = window.screen.orientation.type;
} catch (error) {}
}
但在Safari浏览器中就不行了,因为... orientationchange
根本不支持。即使回调是在改变方向时执行的 window.screen.orientation
是 undefined
.
据我所知,在Safari中,没有一种方法可以让方向改变的回调。我已经在这个问题上研究了两个多星期了,我真的需要任何一种可能有效的解决方法。
结果发现,尽管通常的 window.screen.orientation
是 undefined
在野生动物园 window.orientation - (deprecated)
告诉方向的度数为0、90、-90和180。
我知道这不是一个完美的答案,因为这个功能已经被废弃,但它是(唯一)一个在Safari上工作,如果有人知道一个更好的方法来检测Safari移动网络的方向,然后请分享。