Skip to content

Commit 997a56c

Browse files
committed
feat: 新增Flip动画
1 parent 8dd58ad commit 997a56c

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

Flip/Flip.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Flip {
2+
constructor(children) {
3+
this.#children = [...children]
4+
this.init()
5+
}
6+
7+
#children;
8+
#move = false;
9+
init() {
10+
if (!this.#children) return
11+
12+
this.#children.forEach((child) => {
13+
const rect = child.getBoundingClientRect()
14+
child.startX = rect.left
15+
child.startY = rect.top
16+
})
17+
}
18+
19+
/**
20+
* 播放动画
21+
* @param duration 动画时长 单位:毫秒
22+
*/
23+
play(change, { duration = 500 }) {
24+
if (!this.#children) return
25+
if (this.#move) return
26+
this.#move = true;
27+
28+
change()
29+
30+
this.#children.forEach((child, i) => {
31+
const rect = child.getBoundingClientRect()
32+
const lastX = rect.left
33+
const lastY = rect.top
34+
35+
const animation = child.animate(
36+
[
37+
{
38+
transform: `translate(${child.startX - lastX}px,${child.startY - lastY
39+
}px)`,
40+
},
41+
{
42+
transform: `none`,
43+
},
44+
],
45+
{
46+
duration,
47+
id: `${lastX}-${lastY}-${i}`,
48+
}
49+
)
50+
animation.onfinish = () => {
51+
this.init()
52+
this.#move = false
53+
}
54+
})
55+
}
56+
}
57+
window.Flip = Flip

Flip/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Flip翻转动画

jsconfig.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)