-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (49 loc) · 1.86 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
"use strict"
const toggleSwitch= document.querySelector('input[type="checkbox"]');
const navbar= document.getElementById('nav');
const toggleIcon=document.getElementById('toggle-icon');
const image1= document.getElementById('image1');
const image2= document.getElementById('image2');
const image3= document.getElementById('image3');
const textBox = document.getElementById('text-box');
function toggleLightAndDark(isDark){
navbar.style.backgroundColor = isDark?'rgb(0 0 0 /50%)':'rgb(255 255 255 /50%)';
textBox.style.backgroundColor= isDark ?'rgb(255 255 255 /50%)': 'rgb(0 0 0 /50%)';
toggleIcon.children[0].innerText=isDark ? "Dark Mode":"Light Mode";
isDark ?
toggleIcon.children[1].classList.replace('fa-sun','fa-moon')
:toggleIcon.children[1].classList.replace('fa-moon','fa-sun');
changingImages(isDark ? 'dark' :'light');
}
function changingImages(color){
image1.src= `images/innovative_${color}.svg`
image2.src= `images/feeling_proud_${color}.svg`
image3.src= `images/proud_coder_${color}.svg`
}
function saveTheme(mode){
localStorage.setItem('active_mode',mode);
}
// Switching the theme dynamically
function switchTheme(event){
if(event.target.checked){
saveTheme('dark');
document.documentElement.setAttribute('data-theme','dark');
toggleLightAndDark(true);
}else{
saveTheme('light');
document.documentElement.setAttribute('data-theme','light');
toggleLightAndDark(false);
}
}
// Event Listener
toggleSwitch.addEventListener('change',switchTheme);
// check for the localStorage for the current theme
const currentTheme= localStorage.getItem('active_mode');
// if currentTheme exists
if(currentTheme){
document.documentElement.setAttribute('data-theme',currentTheme);
if(currentTheme==='dark'){
toggleLightAndDark(true);
toggleSwitch.checked=true;
}
}