Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -29,46 +29,53 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 29 |
const navObserver = new IntersectionObserver(entries => {
|
| 30 |
entries.forEach(entry => {
|
| 31 |
if (entry.isIntersecting) {
|
|
|
|
| 32 |
navLinks.forEach(link => {
|
| 33 |
-
link.classList.
|
|
|
|
|
|
|
|
|
|
| 34 |
});
|
| 35 |
}
|
| 36 |
});
|
| 37 |
}, {
|
| 38 |
rootMargin: '-50% 0px -50% 0px'
|
| 39 |
});
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
navObserver.observe(section);
|
| 43 |
-
}
|
| 44 |
});
|
| 45 |
|
| 46 |
-
// --- ★★★★★
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
e.preventDefault(); // 防止連結跳轉
|
| 52 |
|
| 53 |
-
const
|
|
|
|
| 54 |
|
| 55 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
const lightbox = document.createElement('div');
|
| 57 |
-
lightbox.
|
| 58 |
-
|
| 59 |
-
const lightboxContent = document.createElement('img');
|
| 60 |
-
lightboxContent.src = imageUrl;
|
| 61 |
|
| 62 |
-
|
| 63 |
document.body.appendChild(lightbox);
|
| 64 |
-
|
| 65 |
-
//
|
| 66 |
lightbox.addEventListener('click', () => {
|
| 67 |
lightbox.classList.add('closing');
|
| 68 |
setTimeout(() => {
|
| 69 |
-
document.body.
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
});
|
| 72 |
-
}
|
| 73 |
});
|
| 74 |
});
|
|
|
|
| 29 |
const navObserver = new IntersectionObserver(entries => {
|
| 30 |
entries.forEach(entry => {
|
| 31 |
if (entry.isIntersecting) {
|
| 32 |
+
const id = entry.target.getAttribute('id');
|
| 33 |
navLinks.forEach(link => {
|
| 34 |
+
link.classList.remove('active');
|
| 35 |
+
if (link.getAttribute('href') === `#${id}`) {
|
| 36 |
+
link.classList.add('active');
|
| 37 |
+
}
|
| 38 |
});
|
| 39 |
}
|
| 40 |
});
|
| 41 |
}, {
|
| 42 |
rootMargin: '-50% 0px -50% 0px'
|
| 43 |
});
|
| 44 |
+
document.querySelectorAll('section[id]').forEach(section => {
|
| 45 |
+
navObserver.observe(section);
|
|
|
|
|
|
|
| 46 |
});
|
| 47 |
|
| 48 |
+
// --- ★★★★★ 修正並簡化後的燈箱功能 ★★★★★ ---
|
| 49 |
+
document.body.addEventListener('click', function(event) {
|
| 50 |
+
// 檢查點擊的是否是帶有 .lightbox-trigger 的 <a> 標籤
|
| 51 |
+
if (event.target.closest('.lightbox-trigger')) {
|
| 52 |
+
event.preventDefault(); // 阻止連結的默認跳轉行為
|
|
|
|
| 53 |
|
| 54 |
+
const trigger = event.target.closest('.lightbox-trigger');
|
| 55 |
+
const imageUrl = trigger.getAttribute('href');
|
| 56 |
|
| 57 |
+
// 如果頁面上已經有燈箱,就不要再創建
|
| 58 |
+
if (document.querySelector('.lightbox')) {
|
| 59 |
+
return;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// 創建燈箱元素
|
| 63 |
const lightbox = document.createElement('div');
|
| 64 |
+
lightbox.className = 'lightbox';
|
| 65 |
+
lightbox.innerHTML = `<img src="${imageUrl}" alt="放大圖片">`;
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
// 將燈箱加入到 body
|
| 68 |
document.body.appendChild(lightbox);
|
| 69 |
+
|
| 70 |
+
// 監聽點擊燈箱本身以關閉
|
| 71 |
lightbox.addEventListener('click', () => {
|
| 72 |
lightbox.classList.add('closing');
|
| 73 |
setTimeout(() => {
|
| 74 |
+
if (document.body.contains(lightbox)) {
|
| 75 |
+
document.body.removeChild(lightbox);
|
| 76 |
+
}
|
| 77 |
+
}, 300); // 等待淡出動畫結束
|
| 78 |
});
|
| 79 |
+
}
|
| 80 |
});
|
| 81 |
});
|