-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (38 loc) · 1.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const body = document.body;
const pad = (num) => `${num}`.padStart(2, '0');
const setThemeColor = (hex) => {
const meta = document.querySelector('meta[name="theme-color"]');
// update theme color meta content
if (meta) {
meta.content = hex;
} else {
// create theme color meta tag
const _meta = document.createElement('meta');
_meta.name = 'theme-color';
_meta.content = hex;
document.head.appendChild(_meta);
}
}
const getHex = () => {
const date = new Date();
const hour = pad(date.getHours());
const minute = pad(date.getMinutes());
const second = pad(date.getSeconds());
const hex = `#${hour}${minute}${second}`;
return hex;
};
const set = () => {
const current = body.innerText;
const hex = getHex();
if (current !== hex) {
body.innerText = hex;
body.style.backgroundColor = hex;
setThemeColor(hex);
}
requestAnimationFrame(set);
}
set();
// after set called, add background color transition
setTimeout(() => {
body.className = 'transition';
}, 200);