[英]How to get device rotation for iPhone 7 in javascript?
我想为 iPhone 7 及更高版本获取(alpha、beta、gamma)的设备旋转。 对于 iPhone 6,这对我来说效果很好:
window.addEventListener('deviceorientation', function(event) {
console.log(event.alpha);
console.log(event.beta);
console.log(event.gamma);
});
但是对于 iPhone > 6 这个事件不再被触发。 明确地说,我不想要设备方向(纵向/横向),我想要旋转。 最好的情况是绝对(校准)数据。
对于 Android,“deviceorientationabsolute”效果很好。 Apple 世界有类似的东西吗?
感谢和问候, 安德烈亚斯
为了在所有设备中获得设备旋转更好地监听调整大小窗口:
window.addEventListener('resize', function(){
if(window.innerWidth < window.innerHeight){
//portrait
}
if(window.innerWidth > window.innerHeight){
//landscape
}
});
好的,我的解决方案是获得用户许可使用这样的方向。 在那之后'ondeviceorientation'工作。
function startOrientation() {
if (typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission().then(function(response) {
if (response == 'granted') {
console.log('granted');
}
});
} else {
console.log('not granted');
}
}
$("#get_permission").click(function(){
startOrientation();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.