Add dark mode
This commit is contained in:
@@ -1,24 +1,17 @@
|
||||
// Highlight
|
||||
hljs.initHighlightingOnLoad();
|
||||
|
||||
const listBg = document.querySelector('.list-bg');
|
||||
|
||||
if (listBg) {
|
||||
const halfAnHour = 1000 * 60 * 30;
|
||||
|
||||
function setListBgStyle() {
|
||||
const now = new Date();
|
||||
const time = now.getHours() + now.getMinutes() / 60;
|
||||
|
||||
let opacity = Math.abs(time - 14);
|
||||
opacity = opacity > 12 ? 10 + Math.abs(opacity - 14) : opacity;
|
||||
opacity = Number((opacity / 12).toFixed(2));
|
||||
|
||||
listBg.setAttribute('style', `opacity: ${opacity}`);
|
||||
// SunriseSunset
|
||||
const getSunrise = SunriseSunsetJS.getSunrise;
|
||||
const getSunset = SunriseSunsetJS.getSunset;
|
||||
navigator.geolocation.getCurrentPosition((position) => {
|
||||
const latitude = position.coords.latitude;
|
||||
const longitude = position.coords.longitude;
|
||||
const sunrise = getSunrise(latitude, longitude).getTime();
|
||||
const sunset = getSunset(latitude, longitude).getTime();
|
||||
const now = Date.now();
|
||||
if (now < sunrise || now < sunset) {
|
||||
const body = document.querySelector('body');
|
||||
body.classList.add('dark');
|
||||
}
|
||||
|
||||
requestAnimationFrame(setListBgStyle);
|
||||
|
||||
setInterval(() => {
|
||||
requestAnimationFrame(setListBgStyle);
|
||||
}, halfAnHour);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user