我试图通过deviceorientation事件获取alpha,beta和gamma坐标。
我的代码:
window.addEventListener('deviceorientation', function(event) {
alert(event.alpha + ' : ' + event.beta + ' : ' + event.gamma);
});
JS小提琴:https : //jsfiddle.net/myL17nzt/
这对于在设备移动时不断地获取坐标非常有用,但是我只想在页面加载时获取坐标,而不是让它监听运动。我尝试将window.addEventListener更改为window.onload,但这并不能解决问题。
有什么想法我可以做到这一点吗?
谢谢!
最好让监听器将自身删除。
function deviceOrientation(event) {
alert(event.alpha + ' : ' + event.beta + ' : ' + event.gamma);
window.removeEventListener('deviceorientation', deviceOrientation);
}
window.addEventListener('deviceorientation', deviceOrientation);
这样,您就不会因为您不关心的事件而调用它。某些库具有一个称为的函数once
,该函数在第一次事件发生后删除事件侦听器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句