cwadayi commited on
Commit
9c27eec
·
verified ·
1 Parent(s): 92f111a

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +30 -23
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.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
 
46
- // --- ★★★★★ 新增:點擊放大 (Lightbox) 功能 ★★★★★ ---
47
- const lightboxTriggers = document.querySelectorAll('.lightbox-trigger');
48
-
49
- lightboxTriggers.forEach(trigger => {
50
- trigger.addEventListener('click', function(e) {
51
- e.preventDefault(); // 防止連結跳轉
52
 
53
- const imageUrl = this.getAttribute('href');
 
54
 
55
- // 創建燈箱
 
 
 
 
 
56
  const lightbox = document.createElement('div');
57
- lightbox.classList.add('lightbox');
58
-
59
- const lightboxContent = document.createElement('img');
60
- lightboxContent.src = imageUrl;
61
 
62
- lightbox.appendChild(lightboxContent);
63
  document.body.appendChild(lightbox);
64
-
65
- // 點擊燈箱關閉
66
  lightbox.addEventListener('click', () => {
67
  lightbox.classList.add('closing');
68
  setTimeout(() => {
69
- document.body.removeChild(lightbox);
70
- }, 300); // 等待動畫結束後移除
 
 
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
  });