开发者们大家好!我很高兴向大家介绍我的最新项目:待办事项列表应用程序。对于任何希望通过开发实用且广泛使用的工具来提高 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 应用程序的结构,包括用于添加任务的输入字段、用于过滤任务的按钮以及用于显示任务的列表。以下是简要概述:
8b05045a5be5764f313ed5b9168a17e6 49099650ebdc5f3125501fa170048923 93f0f5c25f18dab9d176bd4f6de5d30e 7c8d9f814bcad6a1d7abe4eda5f773e5 acd8feeb3a0ea7477b979779de32785a 26faf3d1af674280d03ba217d87e9421 73257fa2e73da4b56055a059cf2c27e2 af75c476cdb7e6c074ca6da9b40841de 90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0 b2386ffb911b14667cb8f0f91ea547a7ToDo6e916e0f7d1e588d4f442bf645aedb2f 9c3bca370b5104690d9ef395f2c5f8d1 6c04bd5ca3fcae76e30b72ad730ca86d 473a967da286a3c736825b4619e7dc30 e10ab5513b15fdfc05a0ecf2f711572e a683dcf6e0b5ef887480b78331d45d15 16b28748ea4df4d9c2150843fecfba68 c207c066cd0c19ad1de5cb71593baf42 5b40833dfba6231bb262b7cb7d40720a 27edb8eff74dfbe137cb327a54c09cd9 16b28748ea4df4d9c2150843fecfba68 fccd6c3d41d50bdb5145cd7baaf75220 9982b5e8126d3c25c4db469c03af9785 44a095991fd29b28f5ddd6d6881ff2deAll54bdf357c58b8a65c66d7c19c8e4d114 1564f9c588334ed32b77da60b52f207dPending54bdf357c58b8a65c66d7c19c8e4d114 36d459e191cfdecb91f095072ecb0270Completed54bdf357c58b8a65c66d7c19c8e4d114 16b28748ea4df4d9c2150843fecfba68 6f57af0aa841765e232198c3fe8dff4eClear All65281c5ac262bf6d81768915a4a77ac0 16b28748ea4df4d9c2150843fecfba68 afbc96bc18d6d08be58ec6f19ec91ba7929d1f5ca49e04fdcb27f9465b944689 16b28748ea4df4d9c2150843fecfba68 16b28748ea4df4d9c2150843fecfba68 ffd6ba4147bda351239915f463e46e38 e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3 16b28748ea4df4d9c2150843fecfba68 36cc49f0c466276486e50c850b7e4956 73a6ac4ed44ffec12cee46588e518a5e
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 += `e029e5d392d348318f81d313d242be24 5fff4821aebc6f59c799852013eb329d 7d31d2aac35b6939be3087c0de01d3d2 661f0b2a50b24b4897728ef8d70ba230${todo.name}94b3e26ee717c64999d7867364b1b4a3 8c1ecd4bb896b2264e0711597d40766c c6db5d538232746250a0e449769cee05 b65d4e0c206129088f74c31fc3907be972ac96585ae54b6ae11f849d2649d9e6 fa22e1ce105d2afcd0970cbeebc4a51b 8d123e5d0648644572870b71b5ec3fceb9cf7eaaa094f1c8f9c6951a675f823d72ac96585ae54b6ae11f849d2649d9e6Editbed06894275b65c1ab86501b08a632eb a97406ec15980bdd18a31642e9f618a43de93f0a9ec92f094fff8cf50de7dafd72ac96585ae54b6ae11f849d2649d9e6Deletebed06894275b65c1ab86501b08a632eb 929d1f5ca49e04fdcb27f9465b944689 16b28748ea4df4d9c2150843fecfba68 bed06894275b65c1ab86501b08a632eb`; } }); } taskBox.innerHTML = liTag || `45a2772a6b6107b401db3c9b82c049c2You don't have any task here54bdf357c58b8a65c66d7c19c8e4d114`; 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中文网其他相关文章!