-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (65 loc) · 2.47 KB
/
Copy pathscript.js
File metadata and controls
79 lines (65 loc) · 2.47 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
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Personal Website JavaScript
* Handles theme toggling, mobile navigation, and interactive UI logic.
*/
document.addEventListener('DOMContentLoaded', () => {
// 1. Theme Management (Dark / Light mode)
const themeToggleBtn = document.getElementById('theme-toggle');
const htmlRoot = document.documentElement;
// Check saved theme or system preference
const savedTheme = localStorage.getItem('theme');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const initialTheme = savedTheme || (systemPrefersDark ? 'dark' : 'light');
setTheme(initialTheme);
if (themeToggleBtn) {
themeToggleBtn.addEventListener('click', () => {
const currentTheme = htmlRoot.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
});
}
function setTheme(theme) {
htmlRoot.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
}
// 2. Mobile Menu Toggle
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
const navLinks = document.getElementById('nav-links');
if (mobileMenuBtn && navLinks) {
mobileMenuBtn.addEventListener('click', () => {
navLinks.classList.toggle('open');
});
// Close mobile menu when clicking any nav link
navLinks.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', () => {
navLinks.classList.remove('open');
});
});
}
// 3. Dynamic Footer Year
const currentYearSpan = document.getElementById('current-year');
if (currentYearSpan) {
currentYearSpan.textContent = new Date().getFullYear();
}
// 4. Smooth Active Nav Link Highlighting on Scroll
const sections = document.querySelectorAll('section[id]');
const navItems = document.querySelectorAll('.nav-link');
function highlightNavOnScroll() {
const scrollY = window.pageYOffset;
sections.forEach(section => {
const sectionHeight = section.offsetHeight;
const sectionTop = section.offsetTop - 120;
const sectionId = section.getAttribute('id');
if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
navItems.forEach(item => {
if (item.getAttribute('href') === `#${sectionId}`) {
item.classList.add('active');
} else {
item.classList.remove('active');
}
});
}
});
}
window.addEventListener('scroll', highlightNavOnScroll, { passive: true });
});