--- title: deviceorientation slug: Web/API/Window/deviceorientation_event translation_of: Web/API/Window/deviceorientation_event ---

deviceorientation 事件在方向传感器输出新数据的时候触发。其数据系传感器与地球坐标系相比较所得,也就是说在设备上可能会采用设备地磁计的数据。详情参考有关方向与运动信息的说明(Orientation and motion data explained)

常规信息

标准
Orientation
接口
DeviceOrientationEvent
冒泡
可取消
Target
DefaultView (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) {
    // 弹奏一曲夏威夷吉他
};

浏览器兼容性

{{CompatibilityTable}}

功能 Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
基本功能支持 7.0 3.6[1] {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
功能 Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
基本功能支持 3.0 3.6[1] {{CompatNo}} 12 4.2

[1] Firefox 3.6, 4, 和 5 支持 mozOrientation 这一类似 DeviceOrientation 的事件。

相关事件

参见