-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdark_light.html
65 lines (58 loc) · 1.87 KB
/
dark_light.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dark Mode / Light Mode Toggle</title>
<style>
body {
font-family: Arial, sans-serif;
transition: background-color 0.3s, color 0.3s;
}
.container {
text-align: center;
margin-top: 50px;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
body.light-mode {
background-color: #ffffff;
color: #000000;
}
body.dark-mode {
background-color: #000000;
color: #ffffff;
}
</style>
</head>
<body>
<div class="container">
<h1>Dark Mode / Light Mode Toggle</h1>
<button id="toggle-button">Toggle Dark/Light Mode</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const toggleButton = document.getElementById('toggle-button');
// Check if dark mode preference is already saved in localStorage
if (localStorage.getItem('mode') === 'dark') {
document.body.classList.add('dark-mode');
} else {
document.body.classList.add('light-mode');
}
toggleButton.addEventListener('click', function () {
document.body.classList.toggle('dark-mode');
document.body.classList.toggle('light-mode');
// Save the current mode in localStorage
if (document.body.classList.contains('dark-mode')) {
localStorage.setItem('mode', 'dark');
} else {
localStorage.setItem('mode', 'light');
}
});
});
</script>
</body>
</html>