-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d-rotate.html
215 lines (189 loc) · 5.26 KB
/
3d-rotate.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<html>
<head>
<title>3D翻牌效果 </title>
<meta charset="UTF-8">
<script>
window.addEventListener(('orientationchange' in window ? 'orientationchange' : 'resize'), (function () {
function c() {
var d = document.documentElement;
var cw = d.clientWidth || 640;
d.style.fontSize = (20 * (cw / 320)) > 40 ? 40 + 'px' : (20 * (cw / 320)) + 'px';
}
c();
return c;
})(), false);
</script>
<style type="text/css">
/*-webkit-perspective: 800px;*/
/*perspective (透视,视角):属性定义 3D 元素距视图的距离,以像素计。该属性允许您改变 3D 元素查看 3D 元素的视图。决定了你所看到的是2D transform 还是3D transform 。*/
.outer {
width: 100%;
margin: 0 auto;
font-size: 0;
padding: 0 0.5%;
position: relative;
}
.outer .flip {
font-size: 34px;
display: inline-block;
height: 480px;
width: 31%;
margin: 1%;
line-height: 480px;
text-align: center;
position: absolute;
transition: all 1.2s linear;
-webkit-transition: all 1.2s linear;
}
.outer .flip:first-child {
left: 0.5%;
top: 0;
}
.outer .flip:nth-child(2) {
left: 33.5%;
top: 0;
}
.outer .flip:nth-child(3) {
left: 66.5%;
top: 0;
}
.outer .flip:nth-child(4) {
left: 0.5%;
top: 500px;
}
.outer .flip:nth-child(5) {
left: 33.5%;
top: 500px;
}
.outer .flip:nth-child(6) {
left: 66.5%;
top: 500px;
}
.outer .flip.act {
left: 33.5%;
top: 1%;
-webkit-transform: scale(0);
transform: scale(0);
opacity: 0;
}
.outer .flip.act.selected {
-webkit-transform: scale(1);
transform: scale(1);
opacity: 1;
transition: all 1s linear;
-webkit-transition: all 1s linear;
}
.outer .flip div {
width: 100%;
height: 100%;
position: absolute;
/*transform-style 属性规定如何在 3D 空间中呈现被嵌套的元素。默认flat(平的),我们3D效果,然后选择3D。*/
transform-style: preserve-3d;
-webkit-transform-style: preserve-3d;
/*backface-visibility 是否显示该选择元素旋转到背面后的样子。*/
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
transition: all 2s;
-webkit-transition: all 2s;
}
.div1 {
background-color: #1aaefd;
transform: rotateY(0);
-webkit-transform: rotateY(0deg);
}
.div2 {
background-color: #00C000;
transform: rotateY(-180deg);
-webkit-transform: rotateY(-180deg);
}
.flip.active .div1 {
transform: rotateY(-180deg);
-webkit-transform: rotateY(-180deg);
}
.flip.active .div2 {
transform: rotateY(0deg);
-webkit-transform: rotateY(0deg);
}
</style>
</head>
<body>
<div class="outer">
<div class="flip">
<div class="div1">div11111</div>
<div class="div2">div22222</div>
</div>
<div class="flip">
<div class="div1">div11111</div>
<div class="div2">div22222</div>
</div>
<div class="flip">
<div class="div1">div11111</div>
<div class="div2">div22222</div>
</div>
<div class="flip">
<div class="div1">div11111</div>
<div class="div2">div22222</div>
</div>
<div class="flip">
<div class="div1">div11111</div>
<div class="div2">div22222</div>
</div>
<div class="flip">
<div class="div1">div11111</div>
<div class="div2">div22222</div>
</div>
</div>
<script>
(function () {
let flips = document.querySelectorAll('.flip');
let flipsLen = flips.length;
let outer = document.querySelector('.outer');
// 数组乱序
function shuffle(arr) {
let result = [];
while (arr.length) {
let index = ~~(Math.random() * arr.length);
result.push(arr[index]);
arr.splice(index, 1);
}
return result;
}
// 创建一个长度为len的数组,并且每个元素的值等于它的下标
function createArr(len) {
return [...new Array(len).keys()];
}
outer.addEventListener('click', function (e) {
let ep = e.target.parentNode;
console.log(e.target, this, ep);
if (ep.classList.contains('flip')) {
ep.classList.toggle('active');
}
let sleep = function (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, time);
});
};
(async function () {
console.log('start');
await sleep(1000);
let shuffleFlipsIndex = shuffle(createArr(flipsLen));
for (let i = 0; i < flipsLen; i++) {
let time = parseInt(Math.random() * 600);
await sleep(time);
flips[shuffleFlipsIndex[i]].classList.add('active');
console.log(i);
}
await sleep(800);
for (let i = 0; i < flipsLen; i++) {
flips[i].classList.add('act');
}
ep.classList.add('selected');
console.log('end');
})();
});
})();
</script>
</body>
</html>