屏幕亮度
概述
屏幕亮度(Luminosity )API是通过devicelight事件来实现的,通过监测devicelight事件,WEB应用程序可以监视屏幕亮度的变换,在屏幕亮度变换后做一些相应的处理机。
代码示例
浏览器支持检测
通过以下代码可以事先检测浏览器是否支持本API。
if(window. DeviceLightEvent) {
//支持此API
} else {
//不支持此API
}
监视屏幕亮度
window.addEventListener('devicelight', function(event) {
var bodyBg= document.body.style;
//event.value is the lux value returned by the sensor on the device
if (event.value < 100) {
// working in a dark environment
bodyBg.backgroundColor="lightgrey";
} else if (event.value < 1000){
// working in a normal environment
bodyBg.backgroundColor="#fff";
} else }
// working in a bright environment
bodyBg.backgroundColor="darkgray";
}
});