CSS旋转头像特效

发一篇一年前乱写的CSS代码,纯CSS3做的旋转特效,比较简单,看一遍基本就会了。

解读

做这种旋转的关键是弄清楚CSS的元素层级概念,浏览器渲染元素是有规则的,如果有多个兄弟元素被绝对定位,那么后面的元素z-index就比前面的高,backface-visibility也是必须要掌握的,这个属性用的不多,往往在3D场景中才用到,作用是定义当元素不面向屏幕时是否可见,如果在旋转元素时不希望看到其背面,该属性很有用。

CSS

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
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html, body {
height: 100%;
width: 100%;
}
body {
position: relative;
}
.container {
position: absolute;
width: 100%;
height: 250px;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
display: flex;
align-items: center;
background: rgba(237, 209, 196, 0.6);
border-radius: 50%;
}
.rotate-img-list {
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: space-around;
align-items: center;
}
.rotate-img-list > li {
list-style: none;
height: 120px;
width: 120px;
cursor: pointer;
position: relative;
}
.rotate-img-list > li img {
display: block;
width: 100%;
height: auto;
max-width: 100%;
}
.rotate-img-list > li .front,
.rotate-img-list > li .back {
position: absolute;
left: 0;
top: 0;
height: 120px;
width: 120px;
overflow: hidden;
border-radius: 50%;
border: solid 3px #5ddea9;
box-shadow: 0 3px 10px #999;
transition: all 400ms ease-in;
transform-style: preserve-3d;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
/* 定义当元素不面向屏幕时不可见 */
}
.rotate-img-list .front {
transform: rotateY(0deg);
z-index: 2;
}
.rotate-img-list .back {
transform: rotateY(180deg);
z-index: 1;
}
.rotate-img-list .cover {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
/* 不需设置z-index,后面的元素默认层级比前面的高 */
background-color: #000;
border-radius: 50%;
opacity: .65;
pointer-events: none;
}
.rotate-img-list .stars-info {
position: absolute;
top: 0;
width: 114px;
height: 114px;
line-height: 22px;
padding-top: 45px;
text-align: center;
color: #fff;
background: transparent;
border-radius: 50%;
}

/* 转到背面,并调整元素层级 */
.rotate-img-list li:hover .front {
transform: rotateY(180deg);
z-index: 1;
}
/* 转到正面,并调整元素层级 */
.rotate-img-list li:hover .back {
transform: rotateY(0deg);
z-index: 2;
}

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
<div class="container">
<ul class="rotate-img-list">
<li>
<div class="front"><img src="images/t1.png" /></div>
<div class="back">
<img src="images/t1.png" />
<div class="cover"></div>
<div class="stars-info">
Backface
</div>
</div>
</li>
<li>
<div class="front"><img src="images/t2.png" /></div>
<div class="back">
<img src="images/t2.png" />
<div class="cover"></div>
<div class="stars-info">
Backface
</div>
</div>
</li>
<li>
<div class="front"><img src="images/t3.jpg" /></div>
<div class="back">
<img src="images/t3.jpg" />
<div class="cover"></div>
<div class="stars-info">
Backface
</div>
</div>
</li>
</ul>
</div>
✧(≖‿≖)✧ 谢谢您的支持 (●'◡'●)ノ♥
0%