Window: devicemotion 事件
devicemotion 事件會以固定的時間間隔觸發,並指示裝置在當時受到的加速度(包括或不包括重力影響)。如果可用,它還會提供有關旋轉速率的資訊。
此事件不可取消,也不會冒泡。
語法
在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。
js
addEventListener("devicemotion", (event) => { })
ondevicemotion = (event) => { }
事件型別
一個 DeviceMotionEvent。繼承自 Event。
事件屬性
DeviceMotionEvent.acceleration只讀-
一個物件,給出裝置在三個軸(x、y 和 z)上的加速度。加速度以 m/s² 為單位表示。
DeviceMotionEvent.accelerationIncludingGravity只讀-
一個物件,給出裝置在三個軸(x、y 和 z)上的加速度,包括重力影響。加速度以 m/s² 為單位表示。
DeviceMotionEvent.rotationRate只讀-
一個物件,給出裝置在三個方向軸(alpha、beta 和 gamma)上的方向變化速率。旋轉速率以度/秒為單位表示。
DeviceMotionEvent.interval只讀-
一個數字,表示從裝置獲取資料的間隔時間(以毫秒為單位)。
示例
js
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);
規範
| 規範 |
|---|
| 裝置方向和運動 # devicemotion |
| 裝置方向和運動 # dom-window-ondevicemotion |
瀏覽器相容性
載入中…