我将帮你实现一个美观且实用的Todo App,使用HTML、CSS和JavaScript。以下是完整的代码实现,包括添加、删除、完成任务的功能,并使用现代化的设计风格。
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>美观Todo App</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #74ebd5, #acb6e5);
}
.container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
h1 {
text-align: center;
color: #333;
margin-bottom: 20px;
font-size: 24px;
}
.input-container {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
#task-input {
flex: 1;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
outline: none;
transition: border-color 0.3s;
}
#task-input:focus {
border-color: #74ebd5;
}
#add-btn {
padding: 10px 20px;
background: #74ebd5;
border: none;
border-radius: 5px;
color: white;
font-size: 16px;
cursor: pointer;
transition: background 0.3s;
}
#add-btn:hover {
background: #acb6e5;
}
ul {
list-style: none;
}
.task-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background: #f9f9f9;
border-radius: 5px;
margin-bottom: 10px;
transition: transform 0.2s;
}
.task-item:hover {
transform: scale(1.02);
}
.task-item.completed {
background: #e0f7fa;
text-decoration: line-through;
color: #888;
}
.task-item span {
flex: 1;
margin-left: 10px;
}
.task-item input[type="checkbox"] {
cursor: pointer;
}
.delete-btn {
background: #ff6b6b;
border: none;
color: white;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
.delete-btn:hover {
background: #ff8787;
}
@media (max-width: 480px) {
.container {
margin: 20px;
}
h1 {
font-size: 20px;
}
#task-input, #add-btn {
font-size: 14px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>我的待办事项</h1>
<div class="input-container">
<input type="text" id="task-input" placeholder="输入新任务...">
<button id="add-btn">添加</button>
</div>
<ul id="task-list"></ul>
</div>
<script>
const taskInput = document.getElementById('task-input');
const addBtn = document.getElementById('add-btn');
const taskList = document.getElementById('task-list');
// 加载本地存储中的任务
let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
// 初始化任务列表
function renderTasks() {
taskList.innerHTML = '';
tasks.forEach((task, index) => {
const li = document.createElement('li');
li.className = `task-item ${task.completed ? 'completed' : ''}`;
li.innerHTML = `
<input type="checkbox" ${task.completed ? 'checked' : ''} onchange="toggleTask(${index})">
<span>${task.text}</span>
<button class="delete-btn" onclick="deleteTask(${index})">删除</button>
`;
taskList.appendChild(li);
});
saveTasks();
}
// 添加任务
function addTask() {
const taskText = taskInput.value.trim();
if (taskText === '') {
alert('请输入任务内容!');
return;
}
tasks.push({ text: taskText, completed: false });
taskInput.value = '';
renderTasks();
}
// 删除任务
function deleteTask(index) {
tasks.splice(index, 1);
renderTasks();
}
// 切换任务完成状态
function toggleTask(index) {
tasks[index].completed = !tasks[index].completed;
renderTasks();
}
// 保存任务到本地存储
function saveTasks() {
localStorage.setItem('tasks', JSON.stringify(tasks));
}
// 绑定添加按钮事件
addBtn.addEventListener('click', addTask);
// 按回车键添加任务
taskInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addTask();
}
});
// 初始化渲染
renderTasks();
</script>
</body>
</html>