Window: deviceorientation event
The deviceorientation event is fired when fresh data is available from an orientation sensor about the current orientation of the device as compared to the Earth coordinate frame. This data is gathered from a magnetometer inside the device.
See Orientation and motion data explained for details.
This event is not cancelable and does not bubble.
Syntax
Use the event name in addEventListener():
addEventListener("deviceorientation", (event) => { })
Alternatively, assign to the event handler property of the interface:
ondeviceorientation = (event) => { }
Event type
A DeviceOrientationEvent. Inherits from Event.
Examples
if (window.DeviceOrientationEvent) {
window.addEventListener(
"deviceorientation",
(event) => {
const rotateDegrees = event.alpha; // alpha: rotation around z-axis
const leftToRight = event.gamma; // gamma: left to right
const frontToBack = event.beta; // beta: front back motion
handleOrientationEvent(frontToBack, leftToRight, rotateDegrees);
},
true,
);
}
function handleOrientationEvent(frontToBack, leftToRight, rotateDegrees) {
// do something amazing
}
Specifications
Browser compatibility
See also
这篇文档对你有帮助吗?
谢谢你的反馈!