開發者們大家好!我很高興向大家介紹我的最新項目:待辦事項清單應用程式。對於任何希望透過開發實用且廣泛使用的工具來提高 JavaScript 技能的人來說,該專案都是完美的選擇。無論您是剛起步還是希望提升自己的技能,建立待辦事項清單都是學習處理使用者輸入、管理資料和動態更新 DOM 的好方法。
待辦事項清單應用程式是一個簡單但功能強大的工具,可讓使用者有效地管理他們的任務。它具有直覺的介面,使用者可以在其中新增、編輯和刪除任務,將其標記為已完成,並根據任務狀態過濾任務。這個專案是理解 Web 開發核心概念的好方法,包括使用 localStorage 進行事件處理和資料持久化。
以下是專案結構的快速概述:
Todo-List/ ├── index.html ├── styles.css └── script.js
要開始使用待辦事項清單項目,請依照下列步驟操作:
複製儲存庫:
git clone https://github.com/abhishekgurjar-in/Todo-List.git
開啟專案目錄:
cd Todo-List
運行項目:
index.html 檔案提供了 Todo List 應用程式的結構,包括用於新增任務的輸入欄位、用於過濾任務的按鈕以及用於顯示任務的清單。以下是簡要概述:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css" /> <link rel="stylesheet" href="style.css" /> <script src="script.js" defer></script> <title>ToDo</title> </head> <body> <div class="main"> <div id="logo"> <img src="./logo/175e8acd1b69064c1fafe52ed5b12019-removebg-preview.png" alt="" /> </div> <div class="wrapper"> <div class="task-input"> <input type="text" placeholder="Add a new task" /> </div> <div class="controls"> <div class="filters"> <span id="all" class="active">All</span> <span id="pending">Pending</span> <span id="completed">Completed</span> </div> <button class="clear-btn">Clear All</button> </div> <ul class="task-box"></ul> </div> </div> <div class="footer"> <p>Made with ❤️ by Abhishek Gurjar</p> </div> </body> </html>
styles.css 檔案設定待辦事項清單應用程式的樣式,確保簡潔且回應靈敏的設計。以下是一些關鍵樣式:
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { background: #fff; } .main { min-height: 85vh; } #logo { width: 100%; height: 7vh; display: flex; align-items: bottom; justify-content: center; } img { width: 300px; height: 222px; } ::selection { color: #fff; background: #1e293b; } .wrapper { max-width: 405px; background: #64d1ef; margin: 137px auto; padding: 28px 0 30px; border-radius: 7px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.401); } .task-input { height: 52px; padding: 0 25px; position: relative; } .task-input input { height: 100%; width: 100%; outline: none; font-size: 18px; border-radius: 5px; padding: 0 20px 0 10px; border: 1px solid #7a7a7a; } .task-input input:focus, .task-input input.active { padding-left: 10px; border: 2px solid #1e293b; } .task-input input::placeholder { color: #bfbfbf; } .controls, li { display: flex; align-items: center; justify-content: space-between; } .controls { padding: 18px 25px; border-bottom: 1px solid #000000a4; } .filters span { margin: 0 8px; font-size: 17px; color: #444; cursor: pointer; } .filters span:first-child { margin-left: 0; } .filters span.active { color: #101216; } .controls .clear-btn { border: none; opacity: 0.6; outline: none; color: #fff; cursor: pointer; font-size: 13px; padding: 7px 13px; border-radius: 4px; background: #1e293b; letter-spacing: 0.3px; pointer-events: none; transition: transform 0.25s ease; } .clear-btn.active { opacity: 0.9; pointer-events: auto; } .clear-btn:active { transform: scale(0.93); } .task-box { margin-top: 20px; margin-right: 5px; padding: 0 20px 10px 25px; } .task-box.overflow { overflow-y: auto; max-height: 300px; } .task-box::-webkit-scrollbar { width: 5px; } .task-box::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 25px; } .task-box::-webkit-scrollbar-thumb { background: #e6e6e6; border-radius: 25px; } .task-box .task { list-style: none; font-size: 17px; margin-bottom: 18px; padding-bottom: 16px; align-items: flex-start; border-bottom: 1px solid #2c2a2a; } .task-box .task:last-child { margin-bottom: 0; border-bottom: 0; padding-bottom: 0; } .task-box .task label { display: flex; align-items: flex-start; } .task-box label input { margin-top: 7px; accent-color: #1e293b; } .task-box label p { user-select: none; margin-left: 12px; word-wrap: break-word; } .task label p.checked { text-decoration: line-through; } .task-box .settings { position: relative; } .settings :where(i, li) { cursor: pointer; } .settings .task-menu { z-index: 10; right: -5px; bottom: -65px; padding: 5px 0; background: #fff; position: absolute; border-radius: 4px; transform: scale(0); transform-origin: top right; box-shadow: 0 0 6px rgba(0, 0, 0, 0.15); transition: transform 0.2s ease; } .task-box .task:last-child .task-menu { bottom: 0; transform-origin: bottom right; } .task-box .task:first-child .task-menu { bottom: -65px; transform-origin: top right; } .task-menu.show { transform: scale(1); } .task-menu li { height: 25px; font-size: 16px; margin-bottom: 2px; padding: 17px 15px; cursor: pointer; justify-content: flex-start; } .task-menu li:last-child { margin-bottom: 0; } .settings li:hover { background: #f5f5f5; } .settings li i { padding-right: 8px; } .footer { text-align: center; margin: 40px; } @media (max-width: 400px) { body { padding: 0 10px; } .wrapper { padding: 20px 0; } .filters span { margin: 0 5px; } .task-input { padding: 0 20px; } .controls { padding: 18px 20px; } .task-box { margin-top: 20px; margin-right: 5px; padding: 0 15px 10px 20px; } .task label input { margin-top: 4px; } }
script.js 檔案包含新增、編輯、刪除和過濾任務的邏輯。主要功能概述如下:
const taskInput = document.querySelector(".task-input input"), filters = document.querySelectorAll(".filters span"), clearAll = document.querySelector(".clear-btn"), taskBox = document.querySelector(".task-box"); let editId, isEditTask = false, todos = JSON.parse(localStorage.getItem("todo-list")); // Filter tasks based on status (all, completed, pending) filters.forEach(btn => { btn.addEventListener("click", () => { document.querySelector("span.active").classList.remove("active"); btn.classList.add("active"); showTodo(btn.id); }); }); function showTodo(filter) { let liTag = ""; if (todos) { todos.forEach((todo, id) => { let completed = todo.status == "completed" ? "checked" : ""; if (filter == todo.status || filter == "all") { liTag += `<li class="task"> <label for="${id}"> <input onclick="updateStatus(this)" type="checkbox" id="${id}" ${completed}> <p class="${completed}">${todo.name}</p> </label> <div class="settings"> <i onclick="showMenu(this)" class="uil uil-ellipsis-h"></i> <ul class="task-menu"> <li onclick='editTask(${id}, "${todo.name}")'><i class="uil uil-pen"></i>Edit</li> <li onclick='deleteTask(${id}, "${filter}")'><i class="uil uil-trash"></i>Delete</li> </ul> </div> </li>`; } }); } taskBox.innerHTML = liTag || `<span>You don't have any task here</span>`; let checkTask = taskBox.querySelectorAll(".task"); !checkTask.length ? clearAll.classList.remove("active") : clearAll.classList.add("active"); taskBox.offsetHeight >= 300 ? taskBox.classList.add("overflow") : taskBox.classList.remove("overflow"); } showTodo("all"); // Show all tasks by default // Function to show the menu for task options function showMenu(selectedTask) { let menuDiv = selectedTask.parentElement.lastElementChild; menuDiv.classList.add("show"); document.addEventListener("click", e => { if (e.target.tagName != "I" || e.target != selectedTask) { menuDiv.classList.remove("show"); } }); } // Function to update the status of a task (completed or pending) function updateStatus(selectedTask) { let taskName = selectedTask.parentElement.lastElementChild; if (selectedTask.checked) { taskName.classList.add("checked"); todos[selectedTask.id].status = "completed"; } else { taskName.classList.remove("checked"); todos[selectedTask.id].status = "pending"; } localStorage.setItem("todo-list", JSON.stringify(todos)); } // Function to edit an existing task function editTask(taskId, textName) { editId = taskId; isEditTask = true; taskInput.value = textName; taskInput.focus(); taskInput.classList.add("active"); } // Function to delete a task function deleteTask(deleteId, filter) { isEditTask = false; todos.splice(deleteId, 1); localStorage.setItem("todo-list", JSON.stringify(todos)); showTodo(filter); } // Clear all tasks clearAll.addEventListener("click", () => { isEditTask = false; todos.splice(0, todos.length); localStorage.setItem("todo-list", JSON.stringify(todos)); showTodo(); }); // Add a new task or update an existing one taskInput.addEventListener("keyup", e => { let userTask = taskInput.value.trim(); if (e.key == "Enter" && userTask) { if (!isEditTask) { todos = !todos ? [] : todos; let taskInfo = { name: userTask, status: "pending" }; todos.push(taskInfo); } else { isEditTask = false; todos[editId].name = userTask; } taskInput.value = ""; localStorage.setItem("todo-list", JSON.stringify(todos)); showTodo(document.querySelector("span.active").id); } });
在此處查看待辦事項清單應用程式的現場演示。
建立這個待辦事項清單應用程式是一次富有洞察力的經歷,它讓我加深了對 JavaScript、DOM 操作和資料持久性的理解。我希望這個專案能夠為您創建自己的任務管理工具帶來啟發。快樂編碼!
這個專案是我不斷努力掌握 Web 開發的一部分,專注於提高日常生產力的實際應用程式。
以上是建立待辦事項清單網站的詳細內容。更多資訊請關注PHP中文網其他相關文章!