如何解决如何使用JavaScript获取iPhone 7的设备旋转?
我想让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”效果很好。苹果世界有类似的东西吗?
感谢和问候, 安德烈亚斯(Andreas)
解决方法
要使所有设备中的设备旋转,最好在调整大小窗口上收听:
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();
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。