VUE实现“代办事项”
效果图之一
实现步骤:
在文件的路径栏中打开cmd;
使用yarn创建vite项目(js项目);
创建完成启动项目(浏览器预览);
使用Visual Studio Code打开项目文件夹;
清空style.css;
修改App.vue的内容;
<!-- App.vue -->
<template>
<div id="app">
<Todo />
</div>
</template>
<script>
import Todo from './components/Todo.vue';
export default{
name:'App',
components:{
Todo
}
}
</script>
<style>
/* 全局样式 */
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
button:hover {
border-color: #adadad;
background-color: #66ccff;
color: #ffffff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
</style>
在components里,把之前的组件换了(换成Todo.vue);
<!--Todo.vue-->
<template>
<div class="todo-app">
<p class="todo-title">待办事项</p>
<input class="todo-input" maxlength="55" v-model="newTask" @keyup.enter="addTask" placeholder="新增代办事项(Enter确认)" />
<ul class="todo-list">
<li class="todo-item" v-for="(task, index) in filteredTasks" :key="index">
<input type="checkbox" class="todo-checkbox" :id="'task-${index}'" :checked="task.done"
@change="toggleTask(index)" />
<div class="todo-checkbox-custom" :class="{ 'is-checked': task.done }" tabindex="0"
@click="toggleTask(index)"></div>
<label class="todo-label" :for="'task-${index}'" :class="{ 'line-through': task.done }">
{{ task.text }}
</label>
<!-- 注意:这里我们直接在label上使用了:class绑定来添加line-through类 -->
<button class="todo-delete" @click="removeTask(index)">×</button>
</li>
</ul>
<div class="todo-controls">
<div class="todo-stats">
<p> 共 {{ totalTasks }} 条任务</p>
<p> 共 {{ activeTasks }} 条待办</p>
</div>
<button @click="clearCompleted">清空已完成</button>
<button @click="toggleAllTasks">切换所有任务状态</button>
<button @click="setFilter('all')">显示所有</button>
<button @click="setFilter('active')">仅显示待办</button>
<button @click="setFilter('completed')">仅显示已完成</button>
</div>
</div>
</template>
<script setup>
import { ref, computed } from 'vue';
const tasks = ref([]);
const newTask = ref('');
const filter = ref('all'); // 用于控制任务过滤的变量
function addTask() {
if (newTask.value.trim() !== '') {
tasks.value.push({ text: newTask.value, done: false });
newTask.value = '';
}
}
function removeTask(index) {
tasks.value.splice(index, 1);
}
function toggleTask(index) {
tasks.value[index].done = !tasks.value[index].done;
}
function clearCompleted() {
tasks.value = tasks.value.filter(task => !task.done);
}
function toggleAllTasks() {
const areAllDone = tasks.value.every(task => task.done);
tasks.value.forEach(task => {
task.done = !areAllDone;
});
}
function setFilter(filterType) {
filter.value = filterType;
}
const filteredTasks = computed(() => {
if (filter.value === 'all') {
return tasks.value;
} else if (filter.value === 'active') {
return tasks.value.filter(task => !task.done);
} else if (filter.value === 'completed') {
return tasks.value.filter(task => task.done);
}
return []; // 如果没有匹配的过滤器,返回空数组(理论上不会发生)
});
const totalTasks = computed(() => tasks.value.length);
const activeTasks = computed(() => tasks.value.filter(task => !task.done).length);
</script>
<style scoped>
.todo-title {
color: #dc3545;
font-size: 34px;
margin: 10px;
}
.todo-app {
max-width: 600px;
margin: 0 auto;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
background-color: #f8f9fa;
}
.todo-input {
width: calc(100% - 22px);
/* 减去内边距和边框 */
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ced4da;
border-radius: 4px;
font-size: 16px;
}
.todo-list {
list-style-type: none;
/* 移除默认的列表项样式 */
padding: 0;
/* 移除默认的padding */
text-align: left;
/* 任务左对齐 */
}
.todo-item {
display: flex;
/* 使用flex布局来水平排列子元素 */
align-items: center;
/* 子元素在交叉轴上的对齐方式 */
margin-bottom: 10px;
/* 每个代办项之间的间距 */
padding: 10px;
/* 代办项的内边距 */
border: 1px solid #ccc;
/* 边框 */
border-radius: 5px;
/* 边框圆角 */
background-color: #f9f9f9;
/* 背景色 */
}
.todo-checkbox {
display: none;
/* 隐藏实际的复选框 */
}
.todo-checkbox-custom {
cursor: pointer;
width: 20px;
height: 20px;
border-radius: 50%;
/* 设置为圆形 */
border: 2px solid #ccc;
/* 边框样式 */
position: relative;
margin-right: 18px;
/* 与文本保持一些间距 */
background-color: #fff;
/* 背景色 */
}
.todo-checkbox-custom::before {
content: '';
position: absolute;
top: 1px;
left: 1px;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: transparent;
transition: background-color 0.3s;
}
.todo-checkbox:checked+.todo-checkbox-custom::before {
color: #66ccff;
content: '√';
}
.todo-label {
flex-grow: 1;
/* 文本部分占据剩余空间 */
cursor: pointer;
/* 鼠标悬停时显示指针,表示可点击 */
}
.todo-delete {
margin-left: auto;
/* 删除按钮靠右对齐 */
cursor: pointer;
/* 鼠标悬停时显示指针,表示可点击 */
border: none;
/* 移除按钮的默认边框 */
background-color: transparent;
/* 移除按钮的背景色 */
color: #ff5722;
/* 删除按钮的文本颜色 */
font-weight: bold;
/* 字体加粗 */
padding: 0 5px;
/* 内边距,使删除按钮看起来更紧凑 */
}
.todo-delete:hover {
color: #f44336;
/* 鼠标悬停在删除按钮上时改变颜色 */
}
.todo-stats {
width: 95px;
margin-top: 10px;
margin-right: 70px;
text-align: left;
font-size: 12px;
}
.todo-controls {
margin-bottom: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
.todo-controls button {
margin-right: 10px;
/* 除了最后一个按钮外,每个按钮右边都有一些间距 */
}
.todo-controls button:last-child {
margin-right: 0;
/* 最后一个按钮没有右边距 */
}
.line-through {
text-decoration: line-through;
/* 文本被划掉 */
color: #c0c0c0;
}
</style>
保存后刷新页面;
刷新后的默认页面如下图所示: