|
let orientation = prev = 'portrait_up';
window.addEventListener('deviceorientation', function (e) {
if (orientation == 'portrait_up') { // 当前是竖屏,屏幕在home键在上面
if (e.gamma <= -30 && e.beta <= 15) {
orientation = 'landscape_left';
} else if (e.gamma >= 30 && e.beta <= 15) {
orientation = 'landscape_right';
} else {
orientation = 'portrait_up';
}
} else if (orientation == 'landscape_left') { // 当前是横屏,屏幕在home键在左侧
if ((e.gamma >= -15 && e.beta >= 15) || (e.gamma <= -30 && e.beta >= 45)) {
orientation = 'portrait_up';
}
} else if (orientation == 'landscape_right') { // 当前是横屏,屏幕在home键在右侧
if ((e.gamma <= 15 && e.beta >= 15)|| (e.gamma >= 30 && e.beta >= 45)) {
orientation = 'portrait_up';
}
}
test.innerHTML = '<pre>' + JSON.stringify({
orientation: orientation,
alpha: e.alpha,
beta: e.beta,
gamma: e.gamma
}, null, 2) + '<\/pre>';
if(prev != orientation){
prev = orientation;
api.setScreenOrientation({
orientation: orientation
});
}
}, false); |
|