-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (62 loc) · 2.59 KB
/
script.js
File metadata and controls
76 lines (62 loc) · 2.59 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
document.addEventListener('DOMContentLoaded', () => {
// --- Theme Toggling ---
const themeToggleBtn = document.getElementById('theme-toggle');
const htmlElement = document.documentElement;
const STORAGE_KEY = 'tutorial_site_theme';
// Check for saved theme preference or system preference
const savedTheme = localStorage.getItem(STORAGE_KEY);
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (savedTheme) {
htmlElement.setAttribute('data-theme', savedTheme);
} else if (!systemPrefersDark) {
htmlElement.setAttribute('data-theme', 'light');
}
themeToggleBtn.addEventListener('click', () => {
const currentTheme = htmlElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
htmlElement.setAttribute('data-theme', newTheme);
localStorage.setItem(STORAGE_KEY, newTheme);
});
// --- Mobile Menu ---
const mobileMenuBtn = document.getElementById('mobile-menu-btn');
const mobileCloseBtn = document.getElementById('mobile-close-btn');
const sidebar = document.querySelector('.sidebar');
function toggleMenu() {
if (window.innerWidth > 768) {
document.body.classList.toggle('sidebar-hidden');
} else {
sidebar.classList.toggle('open');
}
}
if (mobileMenuBtn) mobileMenuBtn.addEventListener('click', toggleMenu);
if (mobileCloseBtn) mobileCloseBtn.addEventListener('click', toggleMenu);
// Close menu when clicking a link on mobile
const navLinks = document.querySelectorAll('.nav-links a');
navLinks.forEach(link => {
link.addEventListener('click', () => {
if (window.innerWidth <= 768) {
sidebar.classList.remove('open');
}
});
});
// --- Active Link Highlighting on Scroll ---
const sections = document.querySelectorAll('section[id]');
function highlightNavigation() {
const scrollY = window.pageYOffset;
sections.forEach(current => {
const sectionHeight = current.offsetHeight;
const sectionTop = current.offsetTop - 100; // Offset for fixed header
const sectionId = current.getAttribute('id');
const navLink = document.querySelector(`.nav-links a[href="#${sectionId}"]`);
if (navLink) {
if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight) {
document.querySelectorAll('.nav-links a[href^="#"]').forEach(a => a.classList.remove('active'));
navLink.classList.add('active');
}
}
});
}
window.addEventListener('scroll', highlightNavigation);
// --- Copy Code to Clipboard ---
// (Removed as per new content requirements)
});