-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (57 loc) · 2.1 KB
/
script.js
File metadata and controls
67 lines (57 loc) · 2.1 KB
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
/* script.js */
document.addEventListener('DOMContentLoaded', () => {
const toggleThemeCheckbox = document.getElementById('toggle-theme-checkbox');
const navLinks = document.querySelectorAll('nav a');
const sections = document.querySelectorAll('main section');
// --- Theme Toggle Functionality ---
if (toggleThemeCheckbox) {
const applyTheme = () => {
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
document.body.classList.add('light-mode');
toggleThemeCheckbox.checked = true;
} else {
document.body.classList.remove('light-mode');
toggleThemeCheckbox.checked = false;
}
};
applyTheme();
toggleThemeCheckbox.addEventListener('change', () => {
if (toggleThemeCheckbox.checked) {
document.body.classList.add('light-mode');
localStorage.setItem('theme', 'light');
} else {
document.body.classList.remove('light-mode');
localStorage.setItem('theme', 'dark');
}
});
}
// --- Page Navigation Functionality ---
function showPage(pageId) {
sections.forEach(section => {
section.classList.remove('active-section');
});
const targetSection = document.querySelector(pageId);
if (targetSection) {
targetSection.classList.add('active-section');
}
if (history.pushState) {
history.pushState(null, '', pageId);
} else {
window.location.hash = pageId;
}
}
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetId = link.getAttribute('href');
showPage(targetId);
});
});
const initialHash = window.location.hash || '#home';
showPage(initialHash);
window.addEventListener('popstate', () => {
const currentHash = window.location.hash || '#home';
showPage(currentHash);
});
});