cwadayi commited on
Commit
f524f3e
·
verified ·
1 Parent(s): a98b19d

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +26 -53
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
- 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
- });
 
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
+ });