原始文件
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>增強版頁面內導航範例</title>
<style>
body {
font-family: Arial, sans-serif;
/* 移除 CSS 平滑滾動,使用 JavaScript 控制 */
margin: 0;
padding: 0;
}
/* 固定導航欄 */
.navbar {
position: fixed;
top: 0;
width: 100%;
background-color: rgba(51, 51, 51, 0.9);
padding: 10px 0;
text-align: center;
z-index: 1000;
transition: background-color 0.3s;
}
.navbar.scrolled {
background-color: rgba(51, 51, 51, 1);
}
.navbar a {
color: white;
margin: 0 15px;
text-decoration: none;
font-weight: bold;
padding: 8px 16px;
border-radius: 4px;
transition: background-color 0.3s, color 0.3s;
cursor: pointer; /* 更改鼠標樣式以指示可點擊 */
}
.navbar a.active, .navbar a:hover {
background-color: #fff;
color: #333;
}
/* 內容區塊 */
.section {
padding: 100px 20px;
margin-top: 60px; /* 避免被固定導航欄遮蓋 */
min-height: 100vh;
opacity: 0;
transform: translateY(50px);
transition: opacity 1s ease-out, transform 1s ease-out;
}
.section.visible {
opacity: 1;
transform: translateY(0);
}
#section1 { background-color: #f4f4f4; }
#section2 { background-color: #e4e4e4; }
#section3 { background-color: #d4d4d4; }
#section4 { background-color: #c4c4c4; }
/* 返回頂部按鈕 */
#backToTop {
position: fixed;
bottom: 40px;
right: 40px;
background-color: #333;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 50%;
cursor: pointer;
display: none; /* 初始隱藏 */
font-size: 18px;
z-index: 1000;
transition: background-color 0.3s;
}
#backToTop:hover {
background-color: #555;
}
</style>
</head>
<body>
<!-- 導航欄 -->
<div class="navbar" id="navbar">
<a href="#section1" class="nav-link">部分一</a>
<a href="#section2" class="nav-link">部分二</a>
<a href="#section3" class="nav-link">部分三</a>
<a href="#section4" class="nav-link">部分四</a>
</div>
<!-- 內容部分 -->
<div id="section1" class="section">
<h2>部分一</h2>
<p>這是第一部分的內容。你可以在這裡放置任何你想要展示的資訊。</p>
</div>
<div id="section2" class="section">
<h2>部分二</h2>
<p>這是第二部分的內容。繼續添加更多內容,使頁面更豐富。</p>
</div>
<div id="section3" class="section">
<h2>部分三</h2>
<p>這是第三部分的內容。你可以加入圖片、視頻或其他多媒體內容。</p>
</div>
<div id="section4" class="section">
<h2>部分四</h2>
<p>這是第四部分的內容。頁面結束,謝謝你的瀏覽!</p>
</div>
<!-- 返回頂部按鈕 -->
<button id="backToTop" title="返回頂部">⇧</button>
<script>
// 當前視窗滾動位置
const sections = document.querySelectorAll('.section');
const navLinks = document.querySelectorAll('.nav-link');
const navbar = document.getElementById('navbar');
const backToTop = document.getElementById('backToTop');
// 檢查哪些區塊進入視窗,並添加動畫
const observerOptions = {
threshold: 0.1
};
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if(entry.isIntersecting){
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
}, observerOptions);
sections.forEach(section => {
observer.observe(section);
});
// 高亮當前導航連結
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
const sectionTop = section.offsetTop - 70;
if (pageYOffset >= sectionTop) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if(link.getAttribute('href') === `#${current}`){
link.classList.add('active');
}
});
// 導航欄背景變化
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
// 返回頂部按鈕顯示
if (window.scrollY > 300) {
backToTop.style.display = 'block';
} else {
backToTop.style.display = 'none';
}
});
// 返回頂部按鈕功能
backToTop.addEventListener('click', () => {
smoothScrollTo(0, 800); // 800 毫秒滾動到頂部
});
// 自訂平滑滾動函數
function smoothScrollTo(targetPosition, duration) {
const startPosition = window.pageYOffset;
const distance = targetPosition - startPosition;
let startTime = null;
function animation(currentTime) {
if (startTime === null) startTime = currentTime;
const timeElapsed = currentTime - startTime;
const run = ease(timeElapsed, startPosition, distance, duration);
window.scrollTo(0, run);
if (timeElapsed < duration) requestAnimationFrame(animation);
}
// 二次緩和函數
function ease(t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
}
requestAnimationFrame(animation);
}
// 為導航鏈接添加點擊事件,使用自訂平滑滾動
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetId);
const targetPosition = targetSection.offsetTop - 50; // 調整偏移量以適應導航欄高度
smoothScrollTo(targetPosition, 800); // 800 毫秒滾動到目標
});
});
</script>
</body>
</html>
| 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="返回頂部">⇧</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 |