-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-cus-anima.html
79 lines (79 loc) · 1.78 KB
/
03-cus-anima.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 200px;
height: 150px;
border-radius: 20px;
margin: 16px;
background-color: skyblue;
display: flex;
justify-content: center;
align-items: center;
view-transition-name: box;
}
.box.right {
background-color: orangered;
}
@keyframes box-out {
from {
transform: rotateX(0);
opacity: 1;
}
to {
transform: rotateX(180deg);
opacity: 0;
}
}
@keyframes box-in {
from {
transform: rotateX(180deg);
opacity: 0;
}
to {
transform: rotateX(0);
opacity: 1;
}
}
::view-transition-old(box) {
animation-name: box-out;
}
::view-transition-new(box) {
animation-name: box-in;
}
</style>
</head>
<body>
<button onclick="changeDOM()">Change DOM</button>
<div class="container">
<div class="left box">LEFT</div>
</div>
<script>
let isLeft = true;
const createBox = type => {
const box = document.createElement('div');
box.className = `box ${type}`;
box.innerText = type.toUpperCase();
return box;
};
const changeDOM = async () => {
if (!document.startViewTransition) {
switchBox();
return;
}
vt = document.startViewTransition(() => switchBox());
};
const switchBox = () => {
const newBox = isLeft ? createBox('right') : createBox('left');
const container = document.querySelector('.container');
container.innerHTML = '';
container.appendChild(newBox);
isLeft = !isLeft;
};
</script>
</body>
</html>