一、學習目標
今天的重點是讓相簿能夠支援「搜尋功能」,讓使用者可以快速找到想要的照片。隨著上傳的圖片越來越多,如果沒有搜尋系統,使用者要翻找特定圖片會很不方便。因此,本日目標是讓使用者能夠輸入關鍵字(例如分類或檔名)來篩選相簿,達成類似資料庫的檢索效果,讓線上相簿的使用體驗更貼近真實的應用場景。
二、學習過程與方法
在這個階段,我新增了一個輸入框 <input type="text">
作為搜尋欄,並用JavaScript監聽 input
事件,讓搜尋過程能即時反應。當使用者輸入關鍵字時,程式會動態篩選符合條件的圖片(例如檔名或分類中包含輸入文字的圖片)。這個功能的實作主要透過陣列的 filter()
方法完成,邏輯簡單又高效。此外,我也保留原本的分類下拉選單與上傳功能,讓系統在搜尋與分類之間可以靈活切換。
三、實作成果
完成後的系統能夠即時依照輸入關鍵字更新畫面。例如,若使用者輸入「旅遊」,畫面只會顯示屬於旅遊類的圖片;輸入「dog」則會找出所有檔名中包含 dog 的照片。整體搜尋體驗流暢自然,而且搜尋不分大小寫,對使用者非常友善。這樣的功能大幅提升了相簿的互動性與專業度,讓整體作品更像一個真正可用的線上系統。
四、主要程式碼區塊
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>Day12 – 搜尋功能</title>
<style>
/* 全域配色與風格統一 */
:root {
--bg: #ffffff;
--primary: darkorange;
--accent: coral;
--text: #333;
--muted: #777;
--border: #e6e6e6;
--btn: #ff8c42;
--btn-hover: #ff6a00;
--card-bg: #fff;
}
/* 基本頁面設定 */
body {
font-family: "Microsoft JhengHei", Arial, sans-serif;
background-color: var(--bg);
margin: 20px;
text-align: center;
}
h1 {
color: var(--primary);
}
p {
font-size: 18px;
color: var(--accent);
}
input[type="text"], input[type="file"], select {
margin: 10px;
padding: 8px 15px;
border-radius: 8px;
border: 1px solid var(--border);
font-size: 16px;
cursor: pointer;
}
/* 相簿區塊 */
#gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 20px;
margin-top: 20px;
}
/* 圖片卡片樣式 */
.photo-card {
border: 1px solid var(--border);
border-radius: 10px;
padding: 10px;
background: var(--card-bg);
text-align: center;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
position: relative;
transition: 0.3s;
}
.photo-card:hover {
transform: translateY(-3px);
}
.photo-card img {
width: 100%;
border-radius: 10px;
border: 2px solid #ddd;
}
/* 資訊區 */
.info {
font-size: 14px;
color: var(--text);
margin-top: 8px;
line-height: 1.5;
word-wrap: break-word;
overflow-wrap: anywhere;
}
/* 分類標籤 */
.category-tag {
background: var(--accent);
color: white;
border-radius: 5px;
padding: 3px 8px;
font-size: 12px;
position: absolute;
top: 8px;
left: 8px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<h1>我的線上相簿</h1>
<p>Day12:支援搜尋(可依分類或檔名篩選圖片)</p>
<!-- 搜尋框 -->
<input type="text" id="search" placeholder="輸入關鍵字搜尋(分類或檔名)" />
<!-- 分類選擇 -->
<label for="category">選擇分類:</label>
<select id="category">
<option value="旅遊"> 旅遊</option>
<option value="生活"> 生活</option>
<option value="寵物"> 寵物</option>
</select>
<!-- 上傳圖片 -->
<input type="file" id="upload" accept="image/*" multiple>
<!-- 相簿展示 -->
<div id="gallery"></div>
<script>
const upload = document.getElementById("upload");
const gallery = document.getElementById("gallery");
const categorySelect = document.getElementById("category");
const searchInput = document.getElementById("search");
let photos = [];
// 上傳圖片
upload.addEventListener("change", function() {
const category = categorySelect.value;
Array.from(this.files).forEach(file => {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const photoData = {
category: category,
name: file.name,
size: (file.size / 1024).toFixed(1),
width: img.width,
height: img.height,
src: e.target.result
};
photos.push(photoData);
renderGallery(photos);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
});
});
// 搜尋功能
searchInput.addEventListener("input", function() {
const keyword = this.value.toLowerCase();
const filtered = photos.filter(photo =>
photo.category.toLowerCase().includes(keyword) ||
photo.name.toLowerCase().includes(keyword)
);
renderGallery(filtered);
});
// 渲染相簿
function renderGallery(list) {
gallery.innerHTML = "";
list.forEach(photo => {
const card = document.createElement("div");
card.classList.add("photo-card");
const tag = document.createElement("div");
tag.classList.add("category-tag");
tag.textContent = photo.category;
const img = document.createElement("img");
img.src = photo.src;
const info = document.createElement("p");
info.classList.add("info");
const shortName = photo.name.length > 25 ? photo.name.slice(0, 25) + "..." : photo.name;
info.textContent = `分類: ${photo.category} | 檔名: ${shortName} | 大小: ${photo.size} KB | 尺寸: ${photo.width}x${photo.height}`;
card.appendChild(tag);
card.appendChild(img);
card.appendChild(info);
gallery.appendChild(card);
});
}
console.log("Day12:搜尋功能完成");
</script>
</body>
</html>