-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (70 loc) · 2.47 KB
/
script.js
File metadata and controls
81 lines (70 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
80
81
const menuBtn = document.getElementById('menuBtn');
const sidebar = document.getElementById('sidebar');
const main = document.getElementById('main');
const footer = document.getElementById('footer');
const overlay = document.getElementById('overlay');
const scrollTopBtn = document.getElementById('scrollTop');
const isMobile = () => window.innerWidth <= 900;
// Init sidebar state
let sidebarOpen = !isMobile();
function updateLayout() {
if (isMobile()) {
sidebar.classList.remove('collapsed');
sidebar.classList.toggle('open', sidebarOpen);
main.classList.add('expanded');
footer.classList.add('expanded');
overlay.classList.toggle('show', sidebarOpen);
menuBtn.classList.toggle('open', sidebarOpen);
} else {
sidebar.classList.remove('open');
sidebar.classList.toggle('collapsed', !sidebarOpen);
main.classList.toggle('expanded', !sidebarOpen);
footer.classList.toggle('expanded', !sidebarOpen);
overlay.classList.remove('show');
menuBtn.classList.toggle('open', sidebarOpen);
}
}
menuBtn.addEventListener('click', () => {
sidebarOpen = !sidebarOpen;
updateLayout();
});
overlay.addEventListener('click', () => {
sidebarOpen = false;
updateLayout();
});
window.addEventListener('resize', () => {
if (!isMobile()) {
if (!sidebarOpen) sidebarOpen = true;
}
updateLayout();
});
updateLayout();
// Active sidebar item on scroll
const sections = document.querySelectorAll('.topic-section');
const sidebarLinks = document.querySelectorAll('.sidebar-item');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
sidebarLinks.forEach(l => l.classList.remove('active'));
const active = document.querySelector(`.sidebar-item[href="#${entry.target.id}"]`);
if (active) {
active.classList.add('active');
active.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
}
}
});
}, { rootMargin: '-20% 0px -60% 0px' });
sections.forEach(s => observer.observe(s));
// Close sidebar on mobile after clicking link
sidebarLinks.forEach(link => {
link.addEventListener('click', () => {
if (isMobile()) {
sidebarOpen = false;
updateLayout();
}
});
});
// Scroll to top button
window.addEventListener('scroll', () => {
scrollTopBtn.classList.toggle('visible', window.scrollY > 400);
});