示例代码:
if (window.DeviceOrientationEvent) { window.addEventListener("deviceorientation", function(event) { // alpha: 围绕垂直手机屏幕的轴转动的旋转角度 var rotateDegrees = event.alpha; // gamma: 围绕平行充电口的轴转动的旋转角度 var leftToRight = event.gamma; // beta: 围绕平行音量键的轴转动的旋转角度 var frontToBack = event.beta; handleOrientationEvent(frontToBack, leftToRight, rotateDegrees); }, true); } var handleOrientationEvent = function(frontToBack, leftToRight, rotateDegrees) { // 弹奏一曲夏威夷吉他 };
对于浏览器兼容性方面 可以在使用时候参照MDN的说明
总结