就我所見,幾乎所有的移動瀏覽器都出現這種問題,並且沒有簡單的解決方案。
Chrome小組未來一個半官方的建議貼on their blog下解鎖屏幕上的設備方向變化是使用deviceorientation
和模擬一下瀏覽器內部確實找出設備的方向:
var previousDeviceOrientation, currentDeviceOrientation;
window.addEventListener('deviceorientation', function onDeviceOrientationChange(event) {
// event.beta represents a front to back motion of the device and
// event.gamma a left to right motion.
if (Math.abs(event.gamma) > 10 || Math.abs(event.beta) < 10) {
previousDeviceOrientation = currentDeviceOrientation;
currentDeviceOrientation = 'landscape';
return;
}
if (Math.abs(event.gamma) < 10 || Math.abs(event.beta) > 10) {
previousDeviceOrientation = currentDeviceOrientation;
// When device is rotated back to portrait, let's unlock screen orientation.
if (previousDeviceOrientation == 'landscape') {
screen.orientation.unlock();
window.removeEventListener('deviceorientation', onDeviceOrientationChange);
}
}
});
Chrome團隊使用此代碼的特定用例是在使用screen.orientation.lock
(禁用方向更改事件)後獲取設備的方向。
這可以概括爲方向改變事件給你一個微小的時間優勢動畫踢之前的替代品。
棘手的部分是找出了該瀏覽器決定切換方向正確的角度範圍(當瀏覽器沒有實際切換方向時,您不想開始動畫)。
解決此問題的一種方法是使用screen.orientation.lock
完全控制方向更改,其中基本上設置了閾值並相應地鎖定方向。
但是,由於世界並不完美,screen.orientation.lock
只適用於全屏模式或獨立的網絡應用程序......如果您打算讓您的應用程序成爲全屏體驗或獨立的網絡應用程序,那麼您是幸運的。
我正在尋找解決這個相同的問題,你找到一個? – 2017-06-24 08:11:55
@WassimGr我沒有 – kevzettler 2017-06-24 19:03:13
我找到了一個部分解決方案,我會在這裏發佈 – 2017-07-14 05:07:42