Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
| 2 |
const sections = document.querySelectorAll('section');
|
| 3 |
-
|
| 4 |
-
const observer = new IntersectionObserver(entries => {
|
| 5 |
entries.forEach(entry => {
|
| 6 |
if (entry.isIntersecting) {
|
| 7 |
entry.target.classList.add('visible');
|
|
@@ -10,63 +10,36 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 10 |
}, {
|
| 11 |
threshold: 0.1
|
| 12 |
});
|
| 13 |
-
|
| 14 |
sections.forEach(section => {
|
| 15 |
-
|
| 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
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
});
|
| 34 |
|
| 35 |
-
//
|
| 36 |
const navLinks = document.querySelectorAll('.nav-link');
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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 |
-
});
|
|
|
|
| 1 |
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
// --- 區塊淡入動畫 ---
|
| 3 |
const sections = document.querySelectorAll('section');
|
| 4 |
+
const sectionObserver = new IntersectionObserver(entries => {
|
|
|
|
| 5 |
entries.forEach(entry => {
|
| 6 |
if (entry.isIntersecting) {
|
| 7 |
entry.target.classList.add('visible');
|
|
|
|
| 10 |
}, {
|
| 11 |
threshold: 0.1
|
| 12 |
});
|
|
|
|
| 13 |
sections.forEach(section => {
|
| 14 |
+
sectionObserver.observe(section);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
});
|
| 16 |
|
| 17 |
+
// --- 回到頂部按鈕 ---
|
| 18 |
+
const backToTopButton = document.querySelector('.back-to-top');
|
| 19 |
+
window.addEventListener('scroll', () => {
|
| 20 |
+
if (window.pageYOffset > 300) {
|
| 21 |
+
backToTopButton.classList.add('visible');
|
| 22 |
+
} else {
|
| 23 |
+
backToTopButton.classList.remove('visible');
|
| 24 |
+
}
|
| 25 |
});
|
| 26 |
|
| 27 |
+
// --- 導覽列高亮 ---
|
| 28 |
const navLinks = document.querySelectorAll('.nav-link');
|
| 29 |
+
const navObserver = new IntersectionObserver(entries => {
|
| 30 |
+
entries.forEach(entry => {
|
| 31 |
+
if (entry.isIntersecting) {
|
| 32 |
+
navLinks.forEach(link => {
|
| 33 |
+
link.classList.toggle('active', link.getAttribute('href').split('#')[1] === entry.target.id);
|
| 34 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
});
|
| 37 |
+
}, {
|
| 38 |
+
rootMargin: '-50% 0px -50% 0px' // 觸發區域設定在螢幕正中央
|
| 39 |
});
|
| 40 |
+
sections.forEach(section => {
|
| 41 |
+
if(section.id) {
|
| 42 |
+
navObserver.observe(section);
|
| 43 |
+
}
|
| 44 |
+
});
|
| 45 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|