lodash.min.js
<style>
#my{
width: 400px; height: 400px;
border: 2px solid;
}
#my div{
display: inline-block;
width: 40px; height: 40px;
box-sizing: border-box;
border: 1px solid;
text-align: center;
line-height: 40px;
}
/* 如果transition-group中的列表没有发生新增和删除,只是顺序发生了变化, 那么,就不能使用入场和出场动画, 值用v-move即可实现列表位移动画 */
.v-move{
transition: 1s;
}
</style>
</head>
<body>
<script src='https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js'></script>
<!-- 导入数组插件 -->
<script src="./lodash.min.js"></script>
<div id='myApp'>
<button @click="change">变换</button>
<!-- transition-group默认被渲染成span标签, 使用tag修改标签为div -->
<transition-group id="my" tag="div">
<div v-for="item in list" :key="item">{{item}}</div>
</transition-group>
</div>
<script>
new Vue({
el: '#myApp',
data: {
list:[]
},
created() {
for (let i =0; i< 100; i++) {
this.list.push(i)
}
},
methods: {
change(){
// 数组长度不变, 顺序变化也会更新视图
// this.list.reverse();
// 使用插件实现数组乱序
this.list = _.shuffle(this.list)
}
},
})
</script>
</body>