-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (55 loc) · 1.98 KB
/
Copy pathscript.js
File metadata and controls
60 lines (55 loc) · 1.98 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
document.addEventListener('DOMContentLoaded', () => {
// --- Copy to Clipboard with Feedback ---
const copyButtons = document.querySelectorAll('.copy-btn');
copyButtons.forEach(button => {
button.addEventListener('click', async () => {
const codeId = button.getAttribute('data-target');
const codeElement = document.getElementById(codeId);
if (!codeElement) return;
const codeText = codeElement.innerText.trim();
try {
await navigator.clipboard.writeText(codeText);
const originalText = button.innerHTML;
button.innerText = 'Copied! ✓';
button.classList.add('copied');
setTimeout(() => {
button.innerHTML = originalText;
button.classList.remove('copied');
}, 2000);
} catch (err) {
console.error('Failed to copy text: ', err);
}
});
});
// --- Auto-close Mobile Menu on Link Click ---
const mobileMenuCheckbox = document.getElementById('mobile-menu');
const navLinks = document.querySelectorAll('ul.top-menu li a');
navLinks.forEach(link => {
link.addEventListener('click', () => {
if (mobileMenuCheckbox && mobileMenuCheckbox.checked) {
mobileMenuCheckbox.checked = false;
}
});
});
// --- Dynamic Active Navigation Link Highlighting on Scroll ---
const sections = document.querySelectorAll('main section[id], main article[id]');
const navObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const activeId = entry.target.getAttribute('id');
navLinks.forEach(link => {
const href = link.getAttribute('href');
if (href === `#${activeId}`) {
link.classList.add('active');
} else if (href.startsWith('#')) {
link.classList.remove('active');
}
});
}
});
}, {
rootMargin: '-20% 0px -60% 0px',
threshold: 0
});
sections.forEach(section => navObserver.observe(section));
});