todolist动画

new_branch1
roger 2 years ago
parent 5c89477295
commit e723650fd2
  1. 145
      20_脚手架/vue_code/18.src_todolist动画/App.vue
  2. BIN
      20_脚手架/vue_code/18.src_todolist动画/assets/logo.png
  3. 76
      20_脚手架/vue_code/18.src_todolist动画/components/TodoBottom.vue
  4. 49
      20_脚手架/vue_code/18.src_todolist动画/components/TodoHeader.vue
  5. 124
      20_脚手架/vue_code/18.src_todolist动画/components/TodoItem.vue
  6. 57
      20_脚手架/vue_code/18.src_todolist动画/components/TodoList.vue
  7. 19
      20_脚手架/vue_code/18.src_todolist动画/main.js
  8. 140
      20_脚手架/vue_code/src/App.vue
  9. 57
      20_脚手架/vue_code/src/components/Test.vue
  10. 41
      20_脚手架/vue_code/src/components/Test2.vue
  11. 34
      20_脚手架/vue_code/src/components/Test3.vue
  12. 76
      20_脚手架/vue_code/src/components/TodoBottom.vue
  13. 49
      20_脚手架/vue_code/src/components/TodoHeader.vue
  14. 124
      20_脚手架/vue_code/src/components/TodoItem.vue
  15. 57
      20_脚手架/vue_code/src/components/TodoList.vue

@ -0,0 +1,145 @@
<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<TodoHeader @addTodo="addTodo"/>
<TodoList :todos="todos"/>
<TodoBottom :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
</div>
</div>
</div>
</template>
<script>
import pubsub from 'pubsub-js'
import TodoHeader from "./components/TodoHeader.vue";
import TodoList from "./components/TodoList.vue";
import TodoBottom from "./components/TodoBottom.vue";
export default {
name: "App",
components: {
TodoHeader,
TodoList,
TodoBottom
},
data() {
return {
//
todos: JSON.parse(localStorage.getItem('todos')) || []
}
},
// 访App
// App
methods: {
// todoTodoHeader
addTodo(todoObj) {
this.todos.unshift(todoObj)
},
// todo穿TodoListTodoItem
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
},
// todo穿TodoListTodoItem
deleteTodo(_, id) {
this.todos = this.todos.filter(todo => todo.id !== id)
},
// TodoItem
updateTodo(id, title) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.title = title
})
},
// todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
}
},
watch: {
todos: {
deep: true,
handler(value) {
localStorage.setItem('todos', JSON.stringify(value))
}
}
},
// 线
mounted() {
this.$bus.$on('checkTodo', this.checkTodo)
this.$bus.$on('updateTodo', this.updateTodo)
this.pubId = pubsub.subscribe('itemDel', this.deleteTodo) // 使
},
// 线
beforeDestroy() {
this.$bus.$off('checkTodo')
this.$bus.$off('updateTodo')
this.pubsub.unsubscribe(this, pubId) // 使
}
}
</script>
<style>
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-edit {
color: #fff;
background-color: skyblue;
border: 1px solid #82cdea;
margin-right: 5px;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,76 @@
<template>
<div class="todo-bottom" v-show="total">
<label>
<input type="checkbox" v-model="isAll">
</label>
<span>
<span>已完成{{ totalDone }}</span>/全部{{ total }}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "TodoBottom",
// props: ['todos', 'checkAllTodo', 'clearAllTodo'],
props: ['todos'],
computed: {
// todov-show
total() {
return this.todos.length
},
// todo
totalDone() {
return this.todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0)
},
//
// get
// setAppcheckAllTodo
isAll: {
get() {
return this.totalDone === this.total && this.total > 0
},
set(value) {
// this.checkAllTodo(value)
this.$emit('checkAllTodo', value)
}
}
},
methods: {
// AppclearAllTodo
clearAll() {
if (confirm("确认清除全部已完成任务?")) {
this.$emit('clearAllTodo')
}
}
}
}
</script>
<style scoped>
.todo-bottom {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-bottom label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-bottom label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-bottom button {
float: right;
margin-top: 5px;
}
</style>

@ -0,0 +1,49 @@
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车确认" v-model="title" @keyup.en.enter="add">
</div>
</template>
<script>
import {nanoid} from "nanoid"
export default {
name: "TodoHeader",
// props: ['addTodo'],
data() {
// titleplaceholder
return {
title: ''
}
},
methods: {
// todotrim
// Apptodo
add() {
if (!this.title.trim()) return alert("输入不能为空")
const todoObj = {id: nanoid(), title: this.title, done: false}
// this.addTodo(todoObj) // emit
this.$emit('addTodo', todoObj)
this.title = ''
}
}
}
</script>
<style scoped>
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>

@ -0,0 +1,124 @@
<template>
<!-- <transition name="todo" appear>-->
<!-- <li>-->
<!-- <label>-->
<!-- <input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">-->
<!-- <span v-show="!todo.isEdit">{{ todo.title }}</span>-->
<!-- <input v-show="todo.isEdit" type="text" @blur="handleBlur(todo, $event)" ref="inputFocus" :value="todo.title">-->
<!-- </label>-->
<!-- <button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>-->
<!-- <button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">编辑</button>-->
<!-- </li>-->
<!-- </transition>-->
<li>
<label>
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">
<span v-show="!todo.isEdit">{{ todo.title }}</span>
<input v-show="todo.isEdit" type="text" @blur="handleBlur(todo, $event)" ref="inputFocus" :value="todo.title">
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
<button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">编辑</button>
</li>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name: "TodoItem",
// todoListtodoscheckTododeleteTodo
props: ['todo'],
methods: {
// 线app
handleCheck(id) {
this.$bus.$emit('checkTodo', id)
},
//
handleDelete(id) {
if (confirm("确认删除?")) {
pubsub.publish('itemDel', id)
}
},
//
handleEdit(todo) {
if (todo.hasOwnProperty('isEdit')) {
todo.isEdit = true
} else {
this.$set(todo, 'isEdit', true)
}
this.$nextTick(function () {
this.$refs.inputFocus.focus()
})
},
//
handleBlur(todo, e) {
todo.isEdit = false
console.log('updateTodo', todo.id, e.target.value)
if (!e.target.value.trim()) return alert("输入不能为空!")
this.$bus.$emit('updateTodo', todo.id, e.target.value)
}
}
}
</script>
<style scoped>
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
li:hover {
background-color: #ddd;
}
li:hover button {
display: block;
}
/*.todo-enter-active {*/
/* animation: td 0.5s linear;*/
/*}*/
/*.todo-leave-active {*/
/* animation: td 0.5s linear reverse;*/
/*}*/
/*@keyframes td {*/
/* from {*/
/* transform: translateX(100%);*/
/* }*/
/* to {*/
/* transform: translateX(0);*/
/* }*/
/*}*/
</style>

@ -0,0 +1,57 @@
<template>
<ul class="todo-list">
<transition-group name="todo" appear>
<TodoItem
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
/>
</transition-group>
</ul>
</template>
<script>
import TodoItem from "@/components/TodoItem.vue";
export default {
name: "TodoList",
// TodoItem
components: {TodoItem},
props: ['todos']
}
</script>
<style scoped>
.todo-list {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
.todo-enter-active {
animation: td 0.5s linear;
}
.todo-leave-active {
animation: td 0.5s linear reverse;
}
@keyframes td {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
}
</style>

@ -0,0 +1,19 @@
// 引入Vue
import Vue from "vue";
// 引入App
import App from "./App";
// 设置Vue
Vue.config.productionTip = false
// 实例化Vue
new Vue({
components: {
App
},
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this // 安装全局事件总线
}
}).$mount('#app')

@ -1,25 +1,145 @@
<template> <template>
<div> <div id="root">
<Test/> <div class="todo-container">
<Test2/> <div class="todo-wrap">
<Test3/> <TodoHeader @addTodo="addTodo"/>
<TodoList :todos="todos"/>
<TodoBottom :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/>
</div>
</div>
</div> </div>
</template> </template>
<script> <script>
import Test from "./components/Test.vue" import pubsub from 'pubsub-js'
import Test2 from "./components/Test2.vue" import TodoHeader from "./components/TodoHeader.vue";
import Test3 from "./components/Test3.vue" import TodoList from "./components/TodoList.vue";
import TodoBottom from "./components/TodoBottom.vue";
export default { export default {
name: "App", name: "App",
components: { components: {
Test, TodoHeader,
Test2, TodoList,
Test3 TodoBottom
},
data() {
return {
//
todos: JSON.parse(localStorage.getItem('todos')) || []
}
}, },
// 访App
// App
methods: {
// todoTodoHeader
addTodo(todoObj) {
this.todos.unshift(todoObj)
},
// todo穿TodoListTodoItem
checkTodo(id) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done
})
},
// todo穿TodoListTodoItem
deleteTodo(_, id) {
this.todos = this.todos.filter(todo => todo.id !== id)
},
// TodoItem
updateTodo(id, title) {
this.todos.forEach((todo) => {
if (todo.id === id) todo.title = title
})
},
// todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
}
},
watch: {
todos: {
deep: true,
handler(value) {
localStorage.setItem('todos', JSON.stringify(value))
}
}
},
// 线
mounted() {
this.$bus.$on('checkTodo', this.checkTodo)
this.$bus.$on('updateTodo', this.updateTodo)
this.pubId = pubsub.subscribe('itemDel', this.deleteTodo) // 使
},
// 线
beforeDestroy() {
this.$bus.$off('checkTodo')
this.$bus.$off('updateTodo')
this.pubsub.unsubscribe(this, pubId) // 使
}
} }
</script> </script>
<style> <style>
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-edit {
color: #fff;
background-color: skyblue;
border: 1px solid #82cdea;
margin-right: 5px;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style> </style>

@ -1,57 +0,0 @@
<template>
<div>
<button @click="isShow=!isShow">显示/隐藏</button>
<!-- Vue默认实现动画方式 -->
<!-- :appear="true" -->
<transition appear>
<h1 v-show="isShow" class="come">你好啊</h1>
</transition>
<!-- 自定义名称实现动画 -->
<transition name="hello">
<h1 v-show="isShow" class="come">你好啊</h1>
</transition>
</div>
</template>
<script>
export default {
name: "Test",
data() {
return {
isShow: true
}
}
}
</script>
<style scoped>
h1 {
background-color: orange;
width: 300px;
}
/*Vue默认实现动画方式*/
.v-enter-active{
animation: demo 0.5s linear;
}
.v-leave-active{
animation: demo 0.5s linear reverse;
}
/*自定义名称实现动画*/
.hello-enter-active{
animation: demo 0.5s linear;
}
.hello-leave-active{
animation: demo 0.5s linear reverse;
}
@keyframes demo {
from{
transform: translateX(-100%);
}
to{
transform: translateX(0px);
}
}
</style>

@ -1,41 +0,0 @@
<template>
<div>
<button @click="isShow=!isShow">显示/隐藏</button>
<!-- 自定义名称实现动画 -->
<transition-group name="hello" appear>
<h1 v-show="!isShow" class="come" key="1">你好啊</h1>
<h1 v-show="isShow" class="come" key="2">哈哈哈</h1>
</transition-group>
</div>
</template>
<script>
export default {
name: "Test",
data() {
return {
isShow: true
}
}
}
</script>
<style scoped>
h1 {
background-color: orange;
width: 300px;
}
/*过度动画进入的起点和离开的终点*/
.hello-enter,.hello-leave-to{
transform: translateX(-100%);
}
/*过度动画进入的终点和离开的起点*/
.hello-enter-to, .hello-leave{
transform: translateX(0);
}
/*动画效果*/
.hello-enter-active, .hello-leave-active {
transition: 0.5s linear;
}
</style>

@ -1,34 +0,0 @@
<template>
<div>
<button @click="isShow=!isShow">显示/隐藏</button>
<!-- 自定义名称实现动画 -->
<transition-group
appear
name="animate__animated animate__bounce"
enter-active-class="animate__swing"
leave-active-class="animate__backOutUp"
>
<h1 v-show="!isShow" class="come" key="1">你好啊</h1>
<h1 v-show="isShow" class="come" key="2">哈哈哈</h1>
</transition-group>
</div>
</template>
<script>
import 'animate.css'
export default {
name: "Test",
data() {
return {
isShow: true
}
}
}
</script>
<style scoped>
h1 {
background-color: orange;
width: 300px;
}
</style>

@ -0,0 +1,76 @@
<template>
<div class="todo-bottom" v-show="total">
<label>
<input type="checkbox" v-model="isAll">
</label>
<span>
<span>已完成{{ totalDone }}</span>/全部{{ total }}
</span>
<button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div>
</template>
<script>
export default {
name: "TodoBottom",
// props: ['todos', 'checkAllTodo', 'clearAllTodo'],
props: ['todos'],
computed: {
// todov-show
total() {
return this.todos.length
},
// todo
totalDone() {
return this.todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0)
},
//
// get
// setAppcheckAllTodo
isAll: {
get() {
return this.totalDone === this.total && this.total > 0
},
set(value) {
// this.checkAllTodo(value)
this.$emit('checkAllTodo', value)
}
}
},
methods: {
// AppclearAllTodo
clearAll() {
if (confirm("确认清除全部已完成任务?")) {
this.$emit('clearAllTodo')
}
}
}
}
</script>
<style scoped>
.todo-bottom {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-bottom label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-bottom label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-bottom button {
float: right;
margin-top: 5px;
}
</style>

@ -0,0 +1,49 @@
<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车确认" v-model="title" @keyup.en.enter="add">
</div>
</template>
<script>
import {nanoid} from "nanoid"
export default {
name: "TodoHeader",
// props: ['addTodo'],
data() {
// titleplaceholder
return {
title: ''
}
},
methods: {
// todotrim
// Apptodo
add() {
if (!this.title.trim()) return alert("输入不能为空")
const todoObj = {id: nanoid(), title: this.title, done: false}
// this.addTodo(todoObj) // emit
this.$emit('addTodo', todoObj)
this.title = ''
}
}
}
</script>
<style scoped>
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
.todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
</style>

@ -0,0 +1,124 @@
<template>
<!-- <transition name="todo" appear>-->
<!-- <li>-->
<!-- <label>-->
<!-- <input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">-->
<!-- <span v-show="!todo.isEdit">{{ todo.title }}</span>-->
<!-- <input v-show="todo.isEdit" type="text" @blur="handleBlur(todo, $event)" ref="inputFocus" :value="todo.title">-->
<!-- </label>-->
<!-- <button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>-->
<!-- <button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">编辑</button>-->
<!-- </li>-->
<!-- </transition>-->
<li>
<label>
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">
<span v-show="!todo.isEdit">{{ todo.title }}</span>
<input v-show="todo.isEdit" type="text" @blur="handleBlur(todo, $event)" ref="inputFocus" :value="todo.title">
</label>
<button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
<button v-show="!todo.isEdit" class="btn btn-edit" @click="handleEdit(todo)">编辑</button>
</li>
</template>
<script>
import pubsub from 'pubsub-js'
export default {
name: "TodoItem",
// todoListtodoscheckTododeleteTodo
props: ['todo'],
methods: {
// 线app
handleCheck(id) {
this.$bus.$emit('checkTodo', id)
},
//
handleDelete(id) {
if (confirm("确认删除?")) {
pubsub.publish('itemDel', id)
}
},
//
handleEdit(todo) {
if (todo.hasOwnProperty('isEdit')) {
todo.isEdit = true
} else {
this.$set(todo, 'isEdit', true)
}
this.$nextTick(function () {
this.$refs.inputFocus.focus()
})
},
//
handleBlur(todo, e) {
todo.isEdit = false
console.log('updateTodo', todo.id, e.target.value)
if (!e.target.value.trim()) return alert("输入不能为空!")
this.$bus.$emit('updateTodo', todo.id, e.target.value)
}
}
}
</script>
<style scoped>
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
}
li label {
float: left;
cursor: pointer;
}
li label input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button {
float: right;
display: none;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
li:hover {
background-color: #ddd;
}
li:hover button {
display: block;
}
/*.todo-enter-active {*/
/* animation: td 0.5s linear;*/
/*}*/
/*.todo-leave-active {*/
/* animation: td 0.5s linear reverse;*/
/*}*/
/*@keyframes td {*/
/* from {*/
/* transform: translateX(100%);*/
/* }*/
/* to {*/
/* transform: translateX(0);*/
/* }*/
/*}*/
</style>

@ -0,0 +1,57 @@
<template>
<ul class="todo-list">
<transition-group name="todo" appear>
<TodoItem
v-for="todoObj in todos"
:key="todoObj.id"
:todo="todoObj"
/>
</transition-group>
</ul>
</template>
<script>
import TodoItem from "@/components/TodoItem.vue";
export default {
name: "TodoList",
// TodoItem
components: {TodoItem},
props: ['todos']
}
</script>
<style scoped>
.todo-list {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
.todo-enter-active {
animation: td 0.5s linear;
}
.todo-leave-active {
animation: td 0.5s linear reverse;
}
@keyframes td {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
}
</style>
Loading…
Cancel
Save