纯CSS3实现优酷轮播图

CSS3实在是强大, 复杂的动画由CSS来实现着实让人好理解, 闲话不多说直接进入正题。今天实验的不是沉浸式轮播图, 而是类似优酷的立体轮播图


一· 效果演示

这里写图片描述

二· 页面布局

页面布局同正常的轮播图一样没什么好说的。上代码.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- width:500px height:300px -->//我选的图片正常尺寸
<div class="wrapper">
<div class="move">
<img src="1.jpg">
</div>
<div class="move">
<img src="2.jpg">
</div>
<div class="move">
<img src="3.jpg">
</div>
<div class="move">
<img src="4.jpg">
</div>
<div class="move">
<img src="5.jpg">
</div>
</div>

三· 思想说明

这里写图片描述
注意到图片到两边时height应该为0, 但是为了视觉效果图片上还是给了一些高度。
总共轮播一共5种状态, 因此一个animation属性要设置0%, 20%, 40%, 60%, 80%, 100%, 这五种状态。 但是由于开始时盒子内已经有三张起始图片因此我认为应该要写4种运动动画, 3个是开始的图片三个, 第4个动画是通用的。每一次的运动就是分别改变left, height, width, 等几种属性。

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
216
217
218
219
220
221
222
223
224
225
@keyframes start1 {
0% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
20% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
40% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
60% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
80% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
100% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
}
@keyframes start2 {
0% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
20% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
40% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
60% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
80% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
100% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
}
@keyframes start3 {
0% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
20% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
40% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
60% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
80% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
100% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
}
@keyframes start4 {
0% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
20% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
40% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
60% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
80% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
100% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
}

四· 总代码

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>优酷轮播图</title>
<style>
*{
margin: 0;
padding: 0;
}
img{
border:0;
}
ol, ul ,li{list-style: none;}
/* 盒子样式 */
.wrapper {
position: relative;
width: 800px;
/*500 + 200 + 200*/
height: 300px;
margin: 0 auto;
overflow: hidden;
}
.wrapper .move {
position: absolute;
top: 50%;
width: 200px;
float: left;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
.wrapper .move img {
width: 100%;
height: 100%;
}
.wrapper .move:nth-child(1) {
animation: start1 5s ease-in-out infinite;
}
.wrapper .move:nth-child(2) {
animation: start2 5s ease-in-out infinite;
}
.wrapper .move:nth-child(3) {
animation: start3 5s ease-in-out infinite;
}
.wrapper .move:nth-child(4) {
animation: start4 5s ease-in-out infinite;
}
.wrapper .move:nth-child(5) {
animation: start4 5s ease-in-out 1s infinite;
}
@keyframes start1 {
0% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
20% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
40% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
60% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
80% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
100% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
}
@keyframes start2 {
0% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
20% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
40% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
60% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
80% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
100% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
}
@keyframes start3 {
0% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
20% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
40% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
60% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
80% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
100% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
}
@keyframes start4 {
0% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
20% {
top: 50%;
margin-top: -60px;
height: 120px;
width: 200px;
left: 650px;
opacity: 0.3;
z-index: 1;
}
40% {
top:0;
margin-top:0;
height:300px;
width:500px;
left:150px;
opacity:1;
z-index:100;
}
60% {
top:50%;
margin-top:-60px;
height:120px;
width:200px;
left:0px;
opacity:0.3;
z-index:1;
}
80% {
top: 50%;
width: 200px;
opacity: 0.4;
left: -200px;
z-index: 1;
height:0px;
margin-top:0px;
}
100% {
top: 50%;
width: 200px;
opacity: 0.4;
left: 900px;
z-index: 1;
height:0px;
margin-top:0px;
}
}
</style>
</head>
<body>
<!-- width:500px height:300px -->
<div class="wrapper">
<div class="move">
<img src="1.jpg">
</div>
<div class="move">
<img src="2.jpg">
</div>
<div class="move">
<img src="3.jpg">
</div>
<div class="move">
<img src="4.jpg">
</div>
<div class="move">
<img src="5.jpg">
</div>
</div>
</body>
</html>
越来越多的平台(微信公众平台,新浪微博,简书,百度打赏等)支持打赏功能,付费阅读时代越来越近,特此增加了打赏功能,支持微信打赏和支付宝打赏。坚持原创技术分享,您的支持将鼓励我继续创作!