forked from vuejs/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListMove.vue
64 lines (56 loc) · 1.4 KB
/
ListMove.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
<script setup>
import { ref } from 'vue'
const items = ref([1, 2, 3, 4, 5])
let nextNum = items.value.length + 1
function add() {
items.value.splice(randomIndex(), 0, nextNum++)
}
function remove() {
items.value.splice(randomIndex(), 1)
}
function randomIndex() {
return Math.floor(Math.random() * items.value.length)
}
function shuffle(array) {
let currentIndex = array.length
let randomIndex
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--
;[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex]
]
}
return array
}
</script>
<template>
<div class="demo">
<button @click="add">Add</button>
<button @click="remove">Remove</button>
<button @click="shuffle(items)">Shuffle</button>
<TransitionGroup name="list2" tag="ul" style="margin-top: 20px">
<li class="list-item" v-for="item in items" :key="item">
{{ item }}
</li>
</TransitionGroup>
</div>
</template>
<style>
.list2-move, /* apply transition to moving elements */
.list2-enter-active,
.list2-leave-active {
transition: all 0.5s ease;
}
.list2-enter-from,
.list2-leave-to {
opacity: 0;
transform: translateX(30px);
}
/* ensure leaving items are taken out of layout flow so that moving
animations can be calculated correctly. */
.list2-leave-active {
position: absolute !important;
}
</style>