此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

AmbientLightSensor:illuminance 属性

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此特性仅在一些支持的浏览器安全上下文(HTTPS)中可用。

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

AmbientLightSensor 接口的 illuminance 只读属性返回宿主设备周围环境光的当前光照强度,单位为 lux(勒克斯)

一个 Number,表示当前的光照强度,单位为勒克斯(lux)。

示例

js
if ("AmbientLightSensor" in window) {
  const sensor = new AmbientLightSensor();
  sensor.addEventListener("reading", (event) => {
    console.log("当前光照强度:", sensor.illuminance);
  });
  sensor.addEventListener("error", (event) => {
    console.log(event.error.name, event.error.message);
  });
  sensor.start();
}

规范

Specification
Ambient Light Sensor
# ambient-light-sensor-reading-attribute

浏览器兼容性