blob: 7c7ff74ad076ba06262d0a052b7b6e0f57fd131f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
---
title: deviceorientation
slug: Web/API/Window/deviceorientation_event
translation_of: Web/API/Window/deviceorientation_event
---
<p><code>deviceorientation</code> 事件在方向传感器输出新数据的时候触发。其数据系传感器与地球坐标系相比较所得,也就是说在设备上可能会采用设备地磁计的数据。详情参考<a href="/en-US/docs/DOM/Orientation_and_motion_data_explained">有关方向与运动信息的说明(Orientation and motion data explained)</a>。</p>
<h2 id="常规信息">常规信息</h2>
<dl>
<dt style="float: left; text-align: right; width: 120px;">标准</dt>
<dd style="margin: 0 0 0 120px;"><a class="external" href="http://www.w3.org/TR/orientation-event/#deviceorientation">Orientation</a></dd>
<dt style="float: left; text-align: right; width: 120px;">接口</dt>
<dd style="margin: 0 0 0 120px;">DeviceOrientationEvent</dd>
<dt style="float: left; text-align: right; width: 120px;">冒泡</dt>
<dd style="margin: 0 0 0 120px;">否</dd>
<dt style="float: left; text-align: right; width: 120px;">可取消</dt>
<dd style="margin: 0 0 0 120px;">否</dd>
<dt style="float: left; text-align: right; width: 120px;">Target</dt>
<dd style="margin: 0 0 0 120px;">DefaultView (<code>window</code>)</dd>
<dt style="float: left; text-align: right; width: 120px;">默认操作</dt>
<dd style="margin: 0 0 0 120px;">无</dd>
</dl>
<h2 id="属性">属性</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Property</th>
<th scope="col">Type</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>target</code> {{readonlyInline}}</td>
<td>{{domxref("EventTarget")}}</td>
<td>The event target (the topmost target in the DOM tree).</td>
</tr>
<tr>
<td><code>type</code> {{readonlyInline}}</td>
<td>{{domxref("DOMString")}}</td>
<td>The type of event.</td>
</tr>
<tr>
<td><code>bubbles</code> {{readonlyInline}}</td>
<td>{{jsxref("Boolean")}}</td>
<td>Whether the event normally bubbles or not</td>
</tr>
<tr>
<td><code>cancelable</code> {{readonlyInline}}</td>
<td>{{jsxref("Boolean")}}</td>
<td>Whether the event is cancellable or not?</td>
</tr>
<tr>
<td><code>alpha</code> {{readonlyInline}}</td>
<td>double (float)</td>
<td>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.</td>
</tr>
<tr>
<td><code>beta</code> {{readonlyInline}}</td>
<td>double (float)</td>
<td>The current orientation of the device around the X axis; that is, how far the device is tipped forward or backward.</td>
</tr>
<tr>
<td><code>gamma</code> {{readonlyInline}}</td>
<td>double (float)</td>
<td>The current orientation of the device around the Y axis; that is, how far the device is turned left or right.</td>
</tr>
<tr>
<td><code>absolute</code> {{readonlyInline}}</td>
<td>{{jsxref("boolean")}}</td>
<td>This value is <code>true</code> 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 <code>false</code>.</td>
</tr>
</tbody>
</table>
<h2 id="例子">例子</h2>
<pre class="pass: js">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) {
// 弹奏一曲夏威夷吉他
};
</pre>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
{{Compat("api.Window.deviceorientation_event")}}
<h2 id="相关事件">相关事件</h2>
<ul>
<li><a href="/en-US/docs/Web/Events/devicemotion"><code>devicemotion</code></a></li>
</ul>
<h2 id="参见">参见</h2>
<ul>
<li>{{domxref("DeviceMotionEvent")}}</li>
<li>{{domxref("window.ondeviceorientation")}}</li>
<li><a href="/en-US/docs/Web/API/Detecting_device_orientation">检测设备朝向</a></li>
<li><a href="/en-US/docs/DOM/Orientation_and_motion_data_explained">有关方向与运动信息的说明</a></li>
<li>在桌面浏览器中模拟 orientation 事件,基于 <a href="http://louisremi.github.com/orientation-devtool/">orientation-devtool</a></li>
</ul>
|