邻近事件

这是 实验性技术
检查 浏览器兼容性表格 在生产中使用这之前。

proximity events are a handy way to know when a user is close to a device. These events make it possible to react to such a change, for example by shutting down the screen of a smartphone when the user is having a phone call with the device close to their ear.

注意: Obviously, the API requires the device to have a proximity sensor, which are mostly available only on mobile devices. Devices without such a sensor may support those events but will never fire them.

邻近事件

When the device proximity sensor detects a change between the device and an object, it notifies the browser of that change. When the browser gets such a notification, it fires a DeviceProximityEvent for any change, and a UserProximityEvent event in the case of a more rough change.

此事件可以被捕获在 window 对象级别通过使用 addEventListener 方法 (使用 deviceproximity or userproximity 事件名称) 或通过把事件处理程序附加到 window.ondeviceproximity or window.onuserproximity 特性。

Once captured, the event object gives access to different kinds of information:

  • DeviceProximityEvent event provides an exact match for the distance between the device and the object through its value property. It also provides the closest and farthest distance the device is able to detect something through its min and max 特性。
  • UserProximityEvent event provides a rough approximation of the distance, expressed through a boolean. The UserProximityEvent.near 特性为 true if the object is close or false if the object is far.

范例

window.addEventListener('userproximity', function(event) {
  if (event.near) {
    // let's power off the screen
    navigator.mozPower.screenEnabled = false;
  } else {
    // Otherwise, let's power on the screen
    navigator.mozPower.screenEnabled = true;
  }
});
					

规范

规范 状态 Comment
Proximity Sensor
The definition of 'Proximity Events' in that specification.
工作草案 Initial specification

浏览器兼容性

DeviceProximityEvent

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
DeviceProximityEvent
非标
Chrome No Edge No Firefox 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 15 — 61
Opera Android No Safari iOS No Samsung Internet Android No
max
非标
Chrome No Edge No Firefox 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 15 — 61
Opera Android No Safari iOS No Samsung Internet Android No
min
非标
Chrome No Edge No Firefox 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 15 — 61
Opera Android No Safari iOS No Samsung Internet Android No
value
非标
Chrome No Edge No Firefox 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 15 — 61
Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

UserProximityEvent

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
UserProximityEvent
非标
Chrome No Edge No Firefox 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
Opera Android No Safari iOS No Samsung Internet Android No
near
非标
Chrome No Edge No Firefox 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 61
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 62
Disabled
62
Disabled
bug 1462308 .
Disabled From version 62: this feature is behind the device.sensors.proximity.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 15 — 61
Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅