-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
script.js
33 lines (28 loc) · 894 Bytes
/
script.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
const chk = document.getElementById('chk');
const ball = document.querySelector('.ball');
if (localStorage.getItem('theme') === 'dark') {
document.body.classList.add('dark');
ball.classList.add('moveBall');
}
chk.addEventListener('click', () => {
let theme = localStorage.getItem('theme');
console.log(theme);
if (theme === 'dark') {
changeThemeToLight();
ball.style.transform = 'translateX(0)';
} else {
changeThemeToDark();
ball.style.transform = 'translateX(22px)';
}
});
function changeThemeToDark() {
document.body.classList.add('dark');
localStorage.setItem('theme', 'dark');
}
function changeThemeToLight() {
document.body.classList.remove('dark');
localStorage.setItem('theme', 'light');
}
window.addEventListener('load', function () {
(window.matchMedia('(prefers-color-scheme: dark)').matches)?changeThemeToDark():changeThemeToLight();
})