--- title: Eventos de Luz Ambiente slug: Web/API/Eventos_de_Luz_Ambiente tags: - Luz Ambiente translation_of: Web/API/Ambient_Light_Events ---
Os eventos de luz ambiente são uma maneira prática de tornar uma página da Web ou um aplicação ciente de qualquer alteração na intensidade da luz. Isto permite-lhes reagir a essa alteração, por exemplo, alterando o contraste da cor da interface do utilizador (IU) ou alterando a exposição necessária para tirar uma fotografia.
When the light sensor of a device detects a change in the light level, it notifies the browser of that change. When the browser gets such a notification, it fires a {{domxref("DeviceLightEvent")}} event that provides information about the exact light intensity.
This event can be captured at the window object level by using the {{domxref("EventTarget.addEventListener","addEventListener")}} method (using the {{event("devicelight")}} event name) or by attaching an event handler to the {{domxref("window.ondevicelight")}} property.
Once captured, the event object gives access to the light intensity expressed in lux through the {{domxref("DeviceLightEvent.value")}} property.
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 event not supported');
}
| Especificação | Estado | Comentário |
|---|---|---|
| {{SpecName("AmbientLight", "", "Ambient Light Events")}} | {{Spec2("AmbientLight")}} | Initial definition |
{{Compat("api.DeviceLightEvent")}}