Using Light Events

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

环境光线事件是一个易用的让网页或应用感知光强变化的方法。它使网页或应用能对光强变化做出反应,例如改变用户界面的颜色对比度,或为拍照而改变曝光度。

光线事件

当设备的光线传感器检测到光强等级的变化时,它会向浏览器通知这个变化。当浏览器得到这个通知后,会发送(fire)一个提供光强信息的 DeviceLightEvent 事件。

想要捕获这个事件,可用 addEventListener 方法把事件处理器绑定到 window 上(使用 devicelight 事件名),或者直接把事件处理器赋值给 window.ondevicelight 属性。

该事件被捕捉后,可以从传入的事件对象上的 DeviceLightEvent.value 属性获取光强(单位为 lux)。

示例

if ('ondevicelight' in window) {
  window.addEventListener('devicelight', function(event) {
    var body = document.querySelector('body');
    if (event.value < 50) {
      body.classList.add('darklight');
      body.classList.remove('brightlight');
    } else {
      body.classList.add('brightlight');
      body.classList.remove('darklight');
    }
  });
} else {
  console.log('不支持 devicelight 事件');
}

规范

规范 状态 备注
Ambient Light Sensor
Ambient Light Events
Candidate Recommendation 首次定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
DeviceLightEvent
ExperimentalNon-standard
Chrome No support NoEdge Full support YesFirefox Full support 62
Notes Disabled
Full support 62
Notes Disabled
Notes See bug 1462308.
Disabled From version 62: this feature is behind the device.sensors.ambientLight.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 22 — 61
Notes
Notes Not supported for MacBook with Touch Bar and Windows 7 (see bug 754199).
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 62
Notes Disabled
Full support 62
Notes Disabled
Notes See bug 1462308.
Disabled From version 62: this feature is behind the device.sensors.ambientLight.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 15 — 61
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
value
ExperimentalNon-standard
Chrome No support NoEdge Full support 13Firefox Full support 62
Notes Disabled
Full support 62
Notes Disabled
Notes See bug 1462308.
Disabled From version 62: this feature is behind the device.sensors.ambientLight.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 22 — 61
Notes
Notes Not supported for MacBook with Touch Bar and Windows 7 (see bug 754199).
IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 62
Notes Disabled
Full support 62
Notes Disabled
Notes See bug 1462308.
Disabled From version 62: this feature is behind the device.sensors.ambientLight.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
No support 15 — 61
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support  
Full support
No support  
No support
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.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

Gecko 备注

devicelight 事件在 Firefox Mobile for Android (15.0) 和 Firefox OS (B2G) 上实现并默认可用。从Gecko 22.0 (Firefox 22.0 / Thunderbird 22.0 / SeaMonkey 2.19) 开始,Mac OS X桌面版也可使用该事件。

参见