Zuletzt aktiv 10 months ago

這是一個帶有固定導航欄的 HTML 範例,實現了頁面內導航、滾動高亮導航連結、區塊進入視窗時的動畫效果,以及返回頂部按鈕功能,並使用 JavaScript 自訂平滑滾動效果增強用戶體驗。

html_page_with_navigation.html Originalformat
1<!DOCTYPE html>
2<html lang="zh-Hant">
3<head>
4 <meta charset="UTF-8">
5 <title>增強版頁面內導航範例</title>
6 <style>
7 body {
8 font-family: Arial, sans-serif;
9 /* 移除 CSS 平滑滾動,使用 JavaScript 控制 */
10 margin: 0;
11 padding: 0;
12 }
13
14 /* 固定導航欄 */
15 .navbar {
16 position: fixed;
17 top: 0;
18 width: 100%;
19 background-color: rgba(51, 51, 51, 0.9);
20 padding: 10px 0;
21 text-align: center;
22 z-index: 1000;
23 transition: background-color 0.3s;
24 }
25
26 .navbar.scrolled {
27 background-color: rgba(51, 51, 51, 1);
28 }
29
30 .navbar a {
31 color: white;
32 margin: 0 15px;
33 text-decoration: none;
34 font-weight: bold;
35 padding: 8px 16px;
36 border-radius: 4px;
37 transition: background-color 0.3s, color 0.3s;
38 cursor: pointer; /* 更改鼠標樣式以指示可點擊 */
39 }
40
41 .navbar a.active, .navbar a:hover {
42 background-color: #fff;
43 color: #333;
44 }
45
46 /* 內容區塊 */
47 .section {
48 padding: 100px 20px;
49 margin-top: 60px; /* 避免被固定導航欄遮蓋 */
50 min-height: 100vh;
51 opacity: 0;
52 transform: translateY(50px);
53 transition: opacity 1s ease-out, transform 1s ease-out;
54 }
55
56 .section.visible {
57 opacity: 1;
58 transform: translateY(0);
59 }
60
61 #section1 { background-color: #f4f4f4; }
62 #section2 { background-color: #e4e4e4; }
63 #section3 { background-color: #d4d4d4; }
64 #section4 { background-color: #c4c4c4; }
65
66 /* 返回頂部按鈕 */
67 #backToTop {
68 position: fixed;
69 bottom: 40px;
70 right: 40px;
71 background-color: #333;
72 color: #fff;
73 padding: 10px 15px;
74 border: none;
75 border-radius: 50%;
76 cursor: pointer;
77 display: none; /* 初始隱藏 */
78 font-size: 18px;
79 z-index: 1000;
80 transition: background-color 0.3s;
81 }
82
83 #backToTop:hover {
84 background-color: #555;
85 }
86 </style>
87</head>
88<body>
89
90 <!-- 導航欄 -->
91 <div class="navbar" id="navbar">
92 <a href="#section1" class="nav-link">部分一</a>
93 <a href="#section2" class="nav-link">部分二</a>
94 <a href="#section3" class="nav-link">部分三</a>
95 <a href="#section4" class="nav-link">部分四</a>
96 </div>
97
98 <!-- 內容部分 -->
99 <div id="section1" class="section">
100 <h2>部分一</h2>
101 <p>這是第一部分的內容。你可以在這裡放置任何你想要展示的資訊。</p>
102 </div>
103
104 <div id="section2" class="section">
105 <h2>部分二</h2>
106 <p>這是第二部分的內容。繼續添加更多內容,使頁面更豐富。</p>
107 </div>
108
109 <div id="section3" class="section">
110 <h2>部分三</h2>
111 <p>這是第三部分的內容。你可以加入圖片、視頻或其他多媒體內容。</p>
112 </div>
113
114 <div id="section4" class="section">
115 <h2>部分四</h2>
116 <p>這是第四部分的內容。頁面結束,謝謝你的瀏覽!</p>
117 </div>
118
119 <!-- 返回頂部按鈕 -->
120 <button id="backToTop" title="返回頂部">&#8679;</button>
121
122 <script>
123 // 當前視窗滾動位置
124 const sections = document.querySelectorAll('.section');
125 const navLinks = document.querySelectorAll('.nav-link');
126 const navbar = document.getElementById('navbar');
127 const backToTop = document.getElementById('backToTop');
128
129 // 檢查哪些區塊進入視窗,並添加動畫
130 const observerOptions = {
131 threshold: 0.1
132 };
133
134 const observer = new IntersectionObserver((entries, observer) => {
135 entries.forEach(entry => {
136 if(entry.isIntersecting){
137 entry.target.classList.add('visible');
138 observer.unobserve(entry.target);
139 }
140 });
141 }, observerOptions);
142
143 sections.forEach(section => {
144 observer.observe(section);
145 });
146
147 // 高亮當前導航連結
148 window.addEventListener('scroll', () => {
149 let current = '';
150 sections.forEach(section => {
151 const sectionTop = section.offsetTop - 70;
152 if (pageYOffset >= sectionTop) {
153 current = section.getAttribute('id');
154 }
155 });
156
157 navLinks.forEach(link => {
158 link.classList.remove('active');
159 if(link.getAttribute('href') === `#${current}`){
160 link.classList.add('active');
161 }
162 });
163
164 // 導航欄背景變化
165 if (window.scrollY > 50) {
166 navbar.classList.add('scrolled');
167 } else {
168 navbar.classList.remove('scrolled');
169 }
170
171 // 返回頂部按鈕顯示
172 if (window.scrollY > 300) {
173 backToTop.style.display = 'block';
174 } else {
175 backToTop.style.display = 'none';
176 }
177 });
178
179 // 返回頂部按鈕功能
180 backToTop.addEventListener('click', () => {
181 smoothScrollTo(0, 800); // 800 毫秒滾動到頂部
182 });
183
184 // 自訂平滑滾動函數
185 function smoothScrollTo(targetPosition, duration) {
186 const startPosition = window.pageYOffset;
187 const distance = targetPosition - startPosition;
188 let startTime = null;
189
190 function animation(currentTime) {
191 if (startTime === null) startTime = currentTime;
192 const timeElapsed = currentTime - startTime;
193 const run = ease(timeElapsed, startPosition, distance, duration);
194 window.scrollTo(0, run);
195 if (timeElapsed < duration) requestAnimationFrame(animation);
196 }
197
198 // 二次緩和函數
199 function ease(t, b, c, d) {
200 t /= d / 2;
201 if (t < 1) return c / 2 * t * t + b;
202 t--;
203 return -c / 2 * (t * (t - 2) - 1) + b;
204 }
205
206 requestAnimationFrame(animation);
207 }
208
209 // 為導航鏈接添加點擊事件,使用自訂平滑滾動
210 navLinks.forEach(link => {
211 link.addEventListener('click', function(e) {
212 e.preventDefault();
213 const targetId = this.getAttribute('href').substring(1);
214 const targetSection = document.getElementById(targetId);
215 const targetPosition = targetSection.offsetTop - 50; // 調整偏移量以適應導航欄高度
216 smoothScrollTo(targetPosition, 800); // 800 毫秒滾動到目標
217 });
218 });
219 </script>
220</body>
221</html>
222