-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (65 loc) · 2.76 KB
/
script.js
File metadata and controls
77 lines (65 loc) · 2.76 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
document.addEventListener('DOMContentLoaded', () => {
// --- Theme Toggling ---
const themeToggleBtn = document.getElementById('theme-toggle');
const htmlElement = document.documentElement;
const STORAGE_KEY = 'jaseci_tutorial_theme';
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);
document.querySelectorAll('.nav-links a').forEach(link => {
link.addEventListener('click', () => {
if (window.innerWidth <= 768) sidebar.classList.remove('open');
});
});
// --- Active Link on Scroll ---
const sections = document.querySelectorAll('section[id]');
function highlightNavigation() {
const scrollY = window.pageYOffset;
sections.forEach(current => {
const sectionTop = current.offsetTop - 100;
const sectionId = current.getAttribute('id');
// Sidebar navigation
const navLink = document.querySelector(`.nav-links a[href="#${sectionId}"]`);
// On this page navigation
const onThisPageLink = document.querySelector(`.on-this-page a[href="#${sectionId}"]`);
if (scrollY > sectionTop && scrollY <= sectionTop + current.offsetHeight) {
// Update sidebar nav
if (navLink) {
document.querySelectorAll('.nav-links a[href^="#"]').forEach(a => a.classList.remove('active'));
navLink.classList.add('active');
}
// Update on this page nav
if (onThisPageLink) {
document.querySelectorAll('.on-this-page a').forEach(a => a.classList.remove('active'));
onThisPageLink.classList.add('active');
}
}
});
}
window.addEventListener('scroll', highlightNavigation);
// Run once on load to highlight initial section
highlightNavigation();
});