DeviceMotionEvent

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

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 只读
表示从设备获取数据的间隔时间,单位是毫秒。

示例

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

规范

规范 状态 注释
DeviceOrientation Event Specification
DeviceMotionEvent
Editor's Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
DeviceMotionEvent
Experimental
Chrome Full support 11Edge Full support ≤18Firefox Full support 6IE No support NoOpera Full support 15Safari Full support 5WebView Android Full support ≤37Chrome Android Full support 18Firefox Android Full support 6Opera Android Full support 14Safari iOS Full support 4.2Samsung Internet Android Full support 1.0
DeviceMotionEvent() constructor
ExperimentalNon-standard
Chrome Full support 59Edge Full support ≤79Firefox ? IE No support NoOpera ? Safari ? WebView Android Full support 59Chrome Android Full support 59Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android Full support 7.0
acceleration
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support 6IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 6Opera Android ? Safari iOS Full support 4.2Samsung Internet Android Full support Yes
accelerationIncludingGravity
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support 6IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 6Opera Android Full support YesSafari iOS Full support 4.2Samsung Internet Android Full support Yes
interval
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support 6IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 6Opera Android Full support YesSafari iOS Full support 4.2Samsung Internet Android Full support Yes
rotationRate
Experimental
Chrome Full support YesEdge Full support 12Firefox Full support 6IE No support NoOpera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 6Opera Android Full support YesSafari iOS Full support 4.2Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.

参见