vue实战-小案例

加强动手能力是进步的最好方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
<style>
.demo{width: 400px;height: 600px;border: 1px solid #ccc;margin: 0 auto;}
.edit{
display:block ;
width:80%;
height: 35px;
line-height: 35px;
margin: 30px auto;
box-sizing: border-box;
padding-left: 4px;
border-radius: 4px;
border:1px solid #ccc;
outline: 0;
box-shadow: 0 0 5px #ccc;
}
.list{margin: 0 auto;width: 80%}
.unit{position: relative;padding: 10px 0;border-bottom: 1px solid #efefef;}
.unit:last-child{border-bottom: 0;}
.finish{text-decoration:line-through;color: #ccc;}
.del{background: red;
text-decoration: none;
position: absolute;
right:0;
font-size: 12px;
border: 0;
outline: 0;
padding: 2px 5px;
border-radius: 5px;
color: #fff;}
.empty{ font-size: 13px;
color: #ccc;
text-align: center;
padding: 10px 0;}
</style>
</head>
<body>
<div id="app" class="demo">
<input class="edit" v-model="task.content" type="text" placeholder="编写任务" @keydown.enter='addTask' >

<div class="list">
<div class="unit" v-for="(item,index) in list">
<input type="checkbox" @click='change(index)' :checked="item.finished">
<span :class="{finish:item.finished}">{{item.content}}</span>
<button class="del" @click='del(index)'>删除</button>
</div>
<p class="empty" v-if="list.length===0">暂无任务</p>
</div>
</div>
<script>
const vm=new Vue({
el:"#app",
data:{
//默认
task:{
content:'',//内容为空
finished:false,//未完成
deleted:false//未删除
},


//任务列表
list:[]
},


methods:{
addTask(){
this.list.push(this.task);

this.task={
content:'',
finished:false,//是否完成
deleted:false //是否删除
}
},

change(index){
let curstate=this.list[index].finished;
this.list[index].finished=!curstate;
},

del(index){
this.list.splice(index,1)
}
}
})

</script>
</body>
</html>