Window: devicemotion event
{{APIRef("Device Orientation Events")}}{{securecontext_header}}
The devicemotion event is fired at a regular interval and indicates the acceleration rate of the device with/without the contribution of the gravity force at that time. It also provides information about the rate of rotation, if available.
This event is not cancelable and does not bubble.
Syntax
Use the event name in {{domxref("EventTarget/addEventListener", "addEventListener()")}}:
addEventListener("devicemotion", (event) => { })
Alternatively, assign to the event handler property of the interface:
ondevicemotion = (event) => { }
Event type
A {{domxref("DeviceMotionEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("DeviceMotionEvent")}}
Examples
function handleMotionEvent(event) {
const x = event.accelerationIncludingGravity.x;
const y = event.accelerationIncludingGravity.y;
const z = event.accelerationIncludingGravity.z;
// Do something awesome.
}
window.addEventListener("devicemotion", handleMotionEvent);
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- {{domxref("Window.deviceorientation_event", "deviceorientation")}}
这篇文档对你有帮助吗?
谢谢你的反馈!