这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
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;
}
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
Proximity Sensor
The definition of 'Proximity Events' in that specification. |
工作草案 | Initial specification |
DeviceProximityEvent
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
DeviceProximityEvent
非标
|
Chrome No | Edge No |
Firefox
62
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
62
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
max
非标
|
Chrome No | Edge No |
Firefox
62
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
62
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
min
非标
|
Chrome No | Edge No |
Firefox
62
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
62
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
value
非标
|
Chrome No | Edge No |
Firefox
62
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
62
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
见实现注意事项。
用户必须明确启用此特征。
UserProximityEvent
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
UserProximityEvent
非标
|
Chrome No | Edge No |
Firefox
62
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
62
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
near
非标
|
Chrome No | Edge No |
Firefox
62
Disabled
|
IE No | Opera No | Safari No | WebView Android No | Chrome Android No |
Firefox Android
62
Disabled
|
Opera Android No | Safari iOS No | Samsung Internet Android No |
完整支持
不支持
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
见实现注意事项。
用户必须明确启用此特征。