-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (61 loc) · 1.96 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const toggleSwitch=document.querySelector('input[type="checkbox"]');
const nav=document.getElementById('nav');
const toggleIcon=document.getElementById('toggle-icon');
const textBox=document.getElementById('text-box');
function darkMode()
{
nav.style.backgroundColor = 'rgb(0 0 0 / 50%)';
textBox.style.backgroundColor = 'rgb(255 255 255 / 60%)';
// console.log(toggleIcon.children)
toggleIcon.children[0].textContent = 'Dark Mode';
toggleIcon.children[1].classList.replace('fa-sun','fa-moon');
const images=document.getElementsByTagName('img');
for (var i=0; i<images.length;i++)
{
let s1=images[i].src.split('_light').join('_dark').split('/');
let s2=`${s1[3]}/${s1[4]}`;
images[i].src=s2;
}
}
function lightMode()
{
nav.style.backgroundColor = 'rgb(255 255 255 / 50%)';
textBox.style.backgroundColor = 'rgb(0 0 0 / 60%)';
// console.log(toggleIcon.children)
toggleIcon.children[0].textContent = 'Light Mode';
toggleIcon.children[1].classList.replace('fa-moon','fa-sun');
const images=document.getElementsByTagName('img');
for (var i=0; i<images.length;i++)
{
let s1=images[i].src.split('_dark').join('_light').split('/');
let s2=`${s1[3]}/${s1[4]}`;
images[i].src=s2;
}
}
function switchTheme(event)
{
// console.log(event.target.checked)
if(event.target.checked)
{
document.documentElement.setAttribute('data-theme','dark');
darkMode();
localStorage.setItem('theme','dark');
}
else
{
document.documentElement.setAttribute('data-theme','light');
lightMode();
localStorage.setItem('theme','light');
}
}
toggleSwitch.addEventListener('change',switchTheme)
const currentTheme = localStorage.getItem('theme')
if(currentTheme)
{
document.documentElement.setAttribute('data-theme', currentTheme)
if(currentTheme === 'dark')
{
toggleSwitch.checked = true;
darkMode()
}
}