DeviceMotionEvent

Baseline 2023 *
Newly available

Since September 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

* Some parts of this feature may have varying levels of support.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

DeviceMotionEvent 为 web 开发者提供了关于设备的位置和方向的改变速度的信息。

警告: 目前,Firefox 和 Chrome 处理坐标的方式不同。使用时要多加注意。

构造函数

DeviceMotionEvent.DeviceMotionEvent() 非标准

创建一个新的 DeviceMotionEvent

属性

DeviceMotionEvent.acceleration 只读

提供了设备在 X,Y,Z 轴方向上加速度的对象。加速度的单位为 m/s2

DeviceMotionEvent.accelerationIncludingGravity 只读

提供了设备在 X,Y,Z 轴方向上带重力的加速度的对象。加速度的单位为 m/s2

DeviceMotionEvent.rotationRate 只读

提供了设备在 alpha、beta、gamma 轴方向上旋转的速率的对象。旋转速率的单位为度每秒。

DeviceMotionEvent.interval 只读

表示从设备获取数据的间隔时间,单位是毫秒。

示例

js
window.addEventListener("devicemotion", function (event) {
  console.log(event.acceleration.x + " m/s2");
});

规范

Specification
Device Orientation and Motion
# devicemotion

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
DeviceMotionEvent
DeviceMotionEvent() constructor
acceleration
accelerationIncludingGravity
interval
requestPermission() static method
Experimental
rotationRate

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

参见