aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/orphaned/web/api/ambient_light_events/index.html
blob: 79a7ed9fbefc0975ccd6e1e1e674900fd7ee3b1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: Using Light Events
slug: orphaned/Web/API/Ambient_Light_Events
tags:
  - WebAPI
  - 事件
  - 环境光
translation_of: Web/API/Ambient_Light_Events
original_slug: Web/API/Ambient_Light_Events
---
<div>{{DefaultAPISidebar("Ambient Light Events")}}{{SeeCompatTable}}</div>

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

<h2 id="光线事件">光线事件</h2>

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

<p>想要捕获这个事件,可用 {{domxref("EventTarget.addEventListener","addEventListener")}} 方法把事件处理器绑定到 <code>window</code> 上(使用 {{event("devicelight")}} 事件名),或者直接把事件处理器赋值给 {{domxref("window.ondevicelight")}} 属性。</p>

<p>该事件被捕捉后,可以从传入的事件对象上的 {{domxref("DeviceLightEvent.value")}} 属性获取光强(单位为 {{interwiki("wikipedia", "lux")}})。</p>

<h2 id="示例">示例</h2>

<pre class="brush: js">if ('ondevicelight' in window) {
  window.addEventListener('devicelight', function(event) {
    var body = document.querySelector('body');
    if (event.value &lt; 50) {
      body.classList.add('darklight');
      body.classList.remove('brightlight');
    } else {
      body.classList.add('brightlight');
      body.classList.remove('darklight');
    }
  });
} else {
  console.log('不支持 devicelight 事件');
}
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('AmbientLight', '', 'Ambient Light Events') }}</td>
   <td>{{ Spec2('AmbientLight') }}</td>
   <td>首次定义</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.DeviceLightEvent")}}</p>

<h3 id="Gecko_备注">Gecko 备注</h3>

<p>{{event("devicelight")}} 事件在 Firefox Mobile for Android (15.0) 和 Firefox OS (B2G) 上实现并默认可用。从Gecko 22.0 {{geckoRelease("22.0")}} 开始,Mac OS X桌面版也可使用该事件。</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{domxref("DeviceLightEvent")}}</li>
 <li>{{event("devicelight")}}</li>
</ul>