-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathindex.html
111 lines (98 loc) · 3.01 KB
/
index.html
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html>
<head>
<title>Dark Mode Toggle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="app.css">
<style>
body {
background-color: #f6f6f6;
transition: background-color .3s;
}
body[light-mode=dark] {
background-color: #141516;
color: #ced4e2;
}
body[light-mode="dark"] .light-mode-button span:nth-child(1) {
background-color: #ced4e2;
color: #141516;
}
body[light-mode=dark] .light-mode-button span:nth-child(2) {
left: 65px;
background-color: #141516;
}
/* Light Mode Button Styles
--------------------------- */
.light-mode-button {
background: 0;
border: 0;
box-sizing: border-box;
cursor: pointer;
height: 40px;
width: 100px;
position: relative;
border: 1px solid rgba(255, 255, 255, 0);
}
.light-mode-button:focus {
outline: none;
/* Not ideal for accessibility */
}
.light-mode-button span:nth-child(1) {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 40px;
border-radius: 20px;
background-color: #d6d7db;
box-shadow: inset 1px 1px 3px 0 rgb(0 0 0 / 40%);
transition: .3s;
}
.light-mode-button span:nth-child(2) {
position: absolute;
top: 5px;
left: 5px;
width: 30px;
height: 30px;
background-color: #fff;
border-radius: 50%;
box-shadow: 1px 1px 2px 0 rgb(0 0 0 / 40%);
transition: .3s;
}
</style>
</head>
<body>
<script>
window.addEventListener("storage", function () {
if (localStorage.lightMode == "dark") {
app.setAttribute("light-mode", "dark");
} else {
app.setAttribute("light-mode", "light");
}
}, false);
var app = document.getElementsByTagName("BODY")[0];
if (localStorage.lightMode == "dark") {
app.setAttribute("light-mode", "dark");
}
</script>
<div class="container">
<h1>Dark Mode Toggle</h1>
<button class="light-mode-button" aria-label="Toggle Light Mode" onclick="toggle_light_mode()">
<span></span>
<span></span>
</button>
</div>
<script>
function toggle_light_mode() {
var app = document.getElementsByTagName("BODY")[0];
if (localStorage.lightMode == "dark") {
localStorage.lightMode = "light";
app.setAttribute("light-mode", "light");
} else {
localStorage.lightMode = "dark";
app.setAttribute("light-mode", "dark");
}
}
</script>
</body>
</html>