Eventos de Luz Ambiente
Experimental
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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.
Eventos de Luz
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 DeviceLightEvent
(en-US) event that provides information about the exact light intensity.
This event can be captured at the window
object level by using the addEventListener
(en-US) method (using the devicelight
event name) or by attaching an event handler to the window.ondevicelight
(en-US) property.
Once captured, the event object gives access to the light intensity expressed in lux through the DeviceLightEvent.value
(en-US) property.
Exemplo
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ções
Especificação | Estado | Comentário |
---|---|---|
Ambient Light Sensor The definition of 'Ambient Light Events' in that specification. |
Candidate Recommendation | Initial definition |
Compatibilidade de navegador
BCD tables only load in the browser