<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"> <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="../lib/vue.min.js"></script> <link rel="stylesheet" href="../lib/bootstrap.min.css"> <style> li { border: 1px dashed #999; margin: 15px; line-height: 25px; font-size: 12px; } li:hover{ background-color: hotpink; transition: all 0.6s ease; } .v-enter, .v-leave-to{ opacity: 0; transform: translateY(80px); } .v-enter-active, .v-enter-active { transition: all 0.6s ease; } /*下面的 .v-move 和 .v-leave-active 配合使用,能够实现列表后续的元素,渐渐地漂上来*/ .v-move{ transition: all 0.6s ease; } .v-leave-active{ position: absolute; } </style> </head> <body> <div id="app"> <div> Id: <input type="text" v-model="id"> Name: <input type="text" v-model="name"> <input type="button" value="添加" @click="add"> </div> <!-- <ul>--> <!--在实现列表过度的时候,如果需要过渡的元素,是通过 v-for 循环渲染出来的,不能使用transition 包裹,需要使用 transitionGroup--> <!--如果要为 v-for 循环创建元素设置动画,必须为每一个元素设置 :key --> <!--给 transition-group 添加 appear 属性,实现页面入场时候的效果--> <!--通过 为 transition-group 元素,设置 tag 属性,指定 transition-group 渲染为指定的元素,如果不指定 tag 属性,默认渲染为span 标签--> <transition-group appear tag="ul"> <li v-for="(item, i) in list" :key="item.id" @click="del(i)"> {{item.id}}---{{item.name}} </li> </transition-group> <!-- </ul>--> </div> <script> var vm = new Vue({ el: '#app', data: { id: '', name: '', list: [ {id: 1, name: '赵云'}, {id: 2, name: '宫本'}, {id: 3, name: '悟空'}, {id: 4, name: '李白'}, ] }, methods: { add() { this.list.push({id: this.id, name: this.name}) this.id = this.name = '' }, del(i){ this.list.splice(i,1) } } }) </script> </body> </html>