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.

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

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
DeviceOrientation Event Specification
# devicemotion

浏览器兼容性

BCD tables only load in the browser

参见