--- title: deviceorientation slug: Web/API/Window/deviceorientation_event translation_of: Web/API/Window/deviceorientation_event ---
deviceorientation
事件在方向传感器输出新数据的时候触发。其数据系传感器与地球坐标系相比较所得,也就是说在设备上可能会采用设备地磁计的数据。详情参考有关方向与运动信息的说明(Orientation and motion data explained)。
window
)Property | Type | Description |
---|---|---|
target {{readonlyInline}} |
{{domxref("EventTarget")}} | The event target (the topmost target in the DOM tree). |
type {{readonlyInline}} |
{{domxref("DOMString")}} | The type of event. |
bubbles {{readonlyInline}} |
{{jsxref("Boolean")}} | Whether the event normally bubbles or not |
cancelable {{readonlyInline}} |
{{jsxref("Boolean")}} | Whether the event is cancellable or not? |
alpha {{readonlyInline}} |
double (float) | The current orientation of the device around the Z axis; that is, how far the device is rotated around a line perpendicular to the device. |
beta {{readonlyInline}} |
double (float) | The current orientation of the device around the X axis; that is, how far the device is tipped forward or backward. |
gamma {{readonlyInline}} |
double (float) | The current orientation of the device around the Y axis; that is, how far the device is turned left or right. |
absolute {{readonlyInline}} |
{{jsxref("boolean")}} | This value is true if the orientation is provided as a difference between the device coordinate frame and the Earth coordinate frame; if the device can't detect the Earth coordinate frame, this value is false . |
if (window.DeviceOrientationEvent) { window.addEventListener("deviceorientation", function(event) { // alpha: 在Z轴上的角度 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) { // 弹奏一曲夏威夷吉他 };