-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle-switcher.js
87 lines (74 loc) · 2.44 KB
/
style-switcher.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
78
79
80
81
82
83
84
85
86
87
/* ----------------------------------- toggle style switcher------------------------------*/
const styleSwitcherToggler = document.querySelector(".style-switcher-toggler");
styleSwitcherToggler.addEventListener("click", () =>{
document.querySelector(".style-switcher").classList.toggle("open");
})
// hide stylee - switcher on scroll
window.addEventListener("scroll", () =>{
if( document.querySelector(".style-switcher").classList.contains("open")){
document.querySelector(".style-switcher").classList.remove("open");
}
})
/* ----------------------------------------------- theme colors -----------------------------------*/
const alternateStyles = document.querySelectorAll(".alternate-style");
function setActiveStyle(color){
localStorage.setItem("color", color);
console.log(localStorage.getItem("color"));
changeColor();
}
function changeColor(){
alternateStyles.forEach((style) =>{
if(localStorage.getItem("color") === style.getAttribute("title")){
style.removeAttribute("disabled");
}
else{
style.setAttribute("disabled", "true");
}
}
)}
// checking if 'color' key exists
if(localStorage.getItem("color") !== null){
changeColor();
}
/* ----------------------------------------------- light theme and dark theme -----------------------------------*/
const dayNight = document.querySelector(".day-night");
dayNight.addEventListener("click", () =>{
document.body.classList.toggle("dark");
if(document.body.classList.contains("dark")){
localStorage.setItem("theme", "dark");
}
else{
localStorage.setItem("theme", "light");
}
updateIcon();
})
function themeMode(){
if(localStorage.getItem("theme") !== null){
if(localStorage.getItem("theme") === "light"){
document.body.classList.remove("dark");
}
else{
document.body.classList.add("dark");
}
}
updateIcon();
}
themeMode();
function updateIcon(){
if(document.body.classList.contains("dark")){
dayNight.querySelector("i").classList.remove("fa-moon");
dayNight.querySelector("i").classList.add("fa-sun");
}
else{
dayNight.querySelector("i").classList.remove("fa-sun");
dayNight.querySelector("i").classList.add("fa-moon");
}
}
// window.addEventListener("load", () =>{
// if(document.body.classList.contains("dark")){
// dayNight.querySelector("i").classList.add("fa-sun");
// }
// else{
// dayNight.querySelector("i").classList.add("fa-moon");
// }
// })