Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -14,4 +14,59 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 14 |
sections.forEach(section => {
|
| 15 |
observer.observe(section);
|
| 16 |
});
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
sections.forEach(section => {
|
| 15 |
observer.observe(section);
|
| 16 |
});
|
| 17 |
+
|
| 18 |
+
// 可展開內容
|
| 19 |
+
document.querySelectorAll('.toggle-header').forEach(header => {
|
| 20 |
+
header.addEventListener('click', () => {
|
| 21 |
+
const content = header.nextElementSibling;
|
| 22 |
+
content.classList.toggle('active');
|
| 23 |
+
header.classList.toggle('active');
|
| 24 |
+
});
|
| 25 |
+
});
|
| 26 |
+
|
| 27 |
+
// 漢堡選單
|
| 28 |
+
const navToggle = document.querySelector('.nav-toggle');
|
| 29 |
+
const navMenu = document.querySelector('.nav-menu');
|
| 30 |
+
|
| 31 |
+
navToggle.addEventListener('click', () => {
|
| 32 |
+
navMenu.classList.toggle('active');
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
// 導航高亮
|
| 36 |
+
const navLinks = document.querySelectorAll('.nav-link');
|
| 37 |
+
|
| 38 |
+
window.addEventListener('scroll', () => {
|
| 39 |
+
let current = '';
|
| 40 |
+
sections.forEach(section => {
|
| 41 |
+
const sectionTop = section.offsetTop;
|
| 42 |
+
if (scrollY >= sectionTop - 100) {
|
| 43 |
+
current = section.getAttribute('id');
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
navLinks.forEach(link => {
|
| 48 |
+
link.classList.remove('active');
|
| 49 |
+
if (link.getAttribute('href').slice(1) === current) {
|
| 50 |
+
link.classList.add('active');
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
});
|
| 54 |
+
|
| 55 |
+
// 搜尋功能
|
| 56 |
+
function addSearch() {
|
| 57 |
+
const searchInput = document.createElement('input');
|
| 58 |
+
searchInput.type = 'text';
|
| 59 |
+
searchInput.placeholder = '搜尋內容...';
|
| 60 |
+
searchInput.classList.add('search-input');
|
| 61 |
+
document.querySelector('.nav-container').appendChild(searchInput);
|
| 62 |
+
|
| 63 |
+
searchInput.addEventListener('input', (e) => {
|
| 64 |
+
const keyword = e.target.value.toLowerCase();
|
| 65 |
+
sections.forEach(section => {
|
| 66 |
+
const text = section.textContent.toLowerCase();
|
| 67 |
+
section.style.display = text.includes(keyword) ? 'block' : 'none';
|
| 68 |
+
});
|
| 69 |
+
});
|
| 70 |
+
}
|
| 71 |
+
addSearch();
|
| 72 |
+
});
|