diff options
| author | nanxiaobei <nanxiaobei@gmail.com> | 2019-10-15 01:31:07 +0800 |
|---|---|---|
| committer | nanxiaobei <nanxiaobei@gmail.com> | 2019-10-15 01:31:07 +0800 |
| commit | ba297661ff44f2e60c96f0640a4c4d23760826df (patch) | |
| tree | 4d61637a5b3fb97e20b4b47065d63a39ff998802 /static/main.js | |
| parent | 072d2a8c22bc45391a27826b95ee3b811422c3e9 (diff) | |
Add dark mode
Diffstat (limited to 'static/main.js')
| -rw-r--r-- | static/main.js | 35 |
1 files changed, 14 insertions, 21 deletions
diff --git a/static/main.js b/static/main.js index 10d8fe1..dfef446 100644 --- a/static/main.js +++ b/static/main.js @@ -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); -} +}); |
