<template>
<div class="todo-app">
<div class="title">今日事 今日毕</div>
<div class="todo-form">
<input v-model="str" @keydown.enter="add" type="text" class="todo-input" placeholder="请输入待办事项" />
<button @click="add" class="todo-submit">Add todo</button>
</div>
<div v-for="(item, index) in todolist" :key="index" :class="[item.isCompleted ? 'completed item' : 'item']">
<div>
<input v-model="item.isCompleted" type="checkbox" class="checkbox" />
<span class="name">{{ item.name }}</span>
</div>
<div @click="del(index)" class="del" style="color: red">del</div>
</div>
</div>
<!-- 展示v-if -->
</template>
<script setup>
import { ref, watch } from 'vue';
const str = ref("");
const todolist = ref([
{
isCompleted: false,
name: "吃饭"
},
{
isCompleted: false,
name: "睡觉"
},
{
isCompleted: false,
name: "打豆豆"
}
]);
function watchTodolist(){
localStorage.setItem("todolist", JSON.stringify(todolist.value));
}
watch(todolist, watchTodolist, { deep: true });
if(localStorage.getItem("todolist")) {
todolist.value = JSON.parse(localStorage.getItem("todolist"));
}
function add(){
if(str.value == "") return;
todolist.value.push({
isCompleted: false,
name: str.value
});
str.value = "";
}
function del(index){
todolist.value.splice(index, 1);
}
//监听键盘回车键
// window.addEventListener("keydown", function(e){
// if(e.key == "Enter"){
// add();
// }
// })
</script>
<style>
body {
background: linear-gradient(
to right,
rgb(113, 65, 168),
rgba(44, 114, 251, 1)
);
}
.todo-app {
width: 78%;
height: 500px;
margin: 0px auto;
margin-top: 70px;
background: white;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
.title {
text-align: center;
font-size: 30px;
font-weight: 700;
padding: 40px 0;
}
.todo-form {
display: flex;
justify-content: center;
align-items: center;
margin-top: 15px;
margin-bottom: 25px;
}
.todo-input {
width: 60%;
height: 52px;
box-sizing: border-box;
padding: 15px;
border: 1px solid #ccc;
border-radius: 20px 0 0 20px;
outline: none;
}
.todo-submit {
width: 100px;
height: 52px;
box-sizing: border-box;
border: none;
border-radius: 0 20px 20px 0;
background: linear-gradient(
to right,
rgb(113, 65, 168),
rgba(44, 114, 251, 1)
);
color: aliceblue;
cursor: pointer;
}
.item {
display: flex;
align-items: center;
justify-content: space-between;
height: 50px;
box-sizing: border-box;
padding: 16px;
width: 80%;
margin: 10px auto;
border-radius: 10px;
box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 20px;
}
.completed {
text-decoration: line-through;
opacity: 0.4;
}
</style>
TodoList Vue
发布于 2025-02-15 251 次阅读

Comments NOTHING