使用lodash.js完成列表添加、删除、打乱顺序。并在添加、删除、打断顺序时实现过渡效果,效果如下图:
我写出了下面的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>练习5</title>
<script src="../js/vue.js"></script>
<style>
.list-item {
display: inline-block;
margin-right: 10px;
background-color: red;
border-radius: 50%;
width: 25px;
height: 25px;
text-align: center;
line-height: 25px;
color: #fff;
}
.list-enter-active, .list-leave-active {
transition: all 1s;
}
.list-enter, .list-leave-to {
opacity: 0;
transform: translateY(30px);
}
</style>
</head>
<body>
<div id="app">
<button @click="add">随机添加</button>
<button @click="remove">随机删除</button>
<button @click="shuffle">打乱顺序</button>
<transition-group name="'list" tag="p">
<span v-for="item in items" :key="item" class="list-item">
{{item}}
</span>
</transition>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
items: [1,2,3,4,5],
nextNum: 6
},
methods: {
add () {
this.nextNum = Math.floor(Math.random() * 10);
var index = Math.floor(Math.random() * this.items.length);
this.items.splice(index, 0, this.nextNum)
},
remove () {
var index = Math.floor(Math.random() * this.items.length);
this.removedNum = this.items[index];
this.items.splice(index,1)
},
shuffle () {
}
}
})
</script>
</body>
</html>
如何实现打乱顺序的方法和如图给出的样式?