-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (68 loc) · 2.77 KB
/
script.js
File metadata and controls
82 lines (68 loc) · 2.77 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
80
81
82
document.addEventListener('DOMContentLoaded', () => {
// Theme Toggle Logic
const themeToggleBtn = document.getElementById('theme-toggle');
const body = document.body;
// 1. Check Local Storage first (User Preference overrides System)
const savedTheme = localStorage.getItem('theme');
// 2. Check System Preference
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)');
function setTheme(theme) {
body.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
updateThemeIcon(theme);
}
// Initialize Theme
if (savedTheme) {
setTheme(savedTheme);
} else if (systemPrefersDark.matches) {
setTheme('dark');
} else {
setTheme('light');
}
// Handle Toggle Click
themeToggleBtn.addEventListener('click', () => {
const currentTheme = body.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
});
function updateThemeIcon(theme) {
themeToggleBtn.textContent = theme === 'dark' ? 'light_mode' : 'dark_mode';
}
// Active State for Navigation (Based on URL)
const navItems = document.querySelectorAll('.bottom-nav .nav-item, .desktop-nav .nav-link');
const currentPath = window.location.pathname.split('/').pop() || 'index.html';
navItems.forEach(item => {
item.classList.remove('active');
const href = item.getAttribute('href');
if (href === currentPath) {
item.classList.add('active');
}
});
// Top App Bar & Bottom Nav Elevation on Scroll
const topAppBar = document.querySelector('.top-app-bar');
const bottomNav = document.querySelector('.bottom-nav');
function updateScrollState() {
// Top App Bar Logic: Tinted (scrolled) when NOT at top
if (window.scrollY > 0) {
topAppBar.classList.add('scrolled');
} else {
topAppBar.classList.remove('scrolled');
}
// Bottom Nav Logic: Tinted (scrolled) when NOT at bottom
if (bottomNav) {
// Check if we are near the bottom of the page
// Buffer of 10px to handle mobile rounding/overscroll
const isBottom = (window.innerHeight + Math.ceil(window.scrollY)) >= document.body.offsetHeight - 10;
if (!isBottom) {
// Not at bottom -> Tinted (scrolled state)
bottomNav.classList.add('scrolled');
} else {
// At bottom -> Flat (default state)
bottomNav.classList.remove('scrolled');
}
}
}
window.addEventListener('scroll', updateScrollState);
// Initial check in case page is refreshed mid-scroll
updateScrollState();
});