-
Notifications
You must be signed in to change notification settings - Fork 1
/
anima.vue
90 lines (88 loc) · 2.48 KB
/
anima.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<template>
<div>
<div class="container">
<div class="item" v-for="item in arr" :key="item" ref="arr">
{{item}}
</div>
</div>
<button @click="handleClick">click me</button>
</div>
</template>
<script>
export default {
data: ()=>{
return {
arr: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
domarr: [],
prevp: [],
curp: []
}
},
methods: {
handleClick () {
this.getFirst()
console.log(1)
this.arr = this.arr.sort(this.randomsort)
this.$nextTick(()=>{
this.getLast()
this.domarr.forEach((item, index) => {
let prevPosition = this.prevp[index]
let currentPosition = this.curp[index]
const invert = {
left: prevPosition.left - currentPosition.left,
top: prevPosition.top - currentPosition.top,
}
const keyframes = [
{
transform: `translate(${invert.left}px, ${invert.top}px)`,
},
{ transform: "translate(0)" },
]
const options = {
duration: 300,
easing: "cubic-bezier(0,0,0.32,1)",
}
item.animate(keyframes, options)
});
})
},
randomsort() {
return Math.random()>.5 ? -1 : 1;
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
},
getFirst(){
this.prevp = this.domarr.map((dom) => {
const rect = dom.getBoundingClientRect()
const { left, top } = rect
return { left, top }
})
},
getLast(){
this.curp = this.domarr.map((dom) => {
const rect = dom.getBoundingClientRect()
const { left, top } = rect
return { left, top }
})
}
},
mounted() {
this.domarr = this.$refs.arr
},
}
</script>
<style>
.container{
display: flex;
justify-content: space-between;
flex-wrap: wrap;
width: 245px;
}
.item{
text-align: center;
width: 50px;
height: 50px;
line-height: 50px;
border: 2px solid #eee;
margin: 5px 0;
}
</style>