coder-Tom

一个喜欢代码, 音乐,读书, 跑步, 记录生活的程序员

0%

用css实现一些好看的效果

用css实现一个按钮的效果

css是很神奇的一种东西, 你给它一个模板, 告诉它怎么来, 它就给你一个非常漂亮的效果:

switch

比如实现这种开关的效果:

1
2
3
4
5
<label class="switch">
<div class="round"><input name="onoff" id="onoff" type="checkbox">
<div class="back"><label for="onoff" class="but"><span class="on">0</span><span class="off">I</span></label></div>
</div>
</label>

这是html的结构:(本身没有任何生气, 但是使用了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
input {
display: none
}

.on,.off {
position: absolute;
text-align: center;
text-shadow: inset 1px 1px 1px black;
width: 100%;
}

.on {
color: #fff;
top: 10px;
transition: all 0.1s;
font-family: sans-serif
}

.off {
color: #636161;
bottom: 5px;
transition: all 0.1s;
transform: scaleY(0.85);
}

.but {
background-color: #000;
border-radius: 400px 400px 400px 400px / 400px 400px 300px 300px;
border-bottom-width: 0px;
box-shadow: inset 8px 6px 5px -7px rgba(0,0,0,1)
,inset -8px 6px 5px -7px rgba(0,0,0,1)
,inset 0 -3px 2px -2px rgba(200,200,200,.5)
,0 3px 3px -2px rgba(0,0,0,1)
,inset 0 -230px 60px -200px rgba(255,255,255,.2)
,inset 0 220px 40px -200px rgba(0,0,0,.3);
display: block;
font-size: 17px;
height: 60px;
position: relative;
transition: all 0.2s;
width: 40px;
}

.back {
background-color: #000;
background-image: -webkit-linear-gradient(0deg, transparent 30%, transparent 65%)
,-webkit-linear-gradient(0deg, rgba(245, 245, 245, 0) 30%, rgba(150,150,150,.1) 50%, rgba(150,150,150,0) 70%);
border-radius: 115px;
box-sizing: border-box;
height: 85px;
padding: 4px 4px;
transition: all 0.2s;
width: 50px;
}

input:checked + .back .on,input:checked + .back .off {
text-shadow: inset 1px 1px 1px black;
}

input:checked + .back .on {
color: rgb(141, 141, 141);
top: 10px;
transform: scaleY(0.85);
}

input:checked + .back .off {
color: #fff;
bottom: 5px;
transform: scaleY(1);
}

input:checked + .back .but {
background: #232323;
background-image: -webkit-radial-gradient(55% 18%,circle closest-corner,rgba(0,0,0,.3) ,rgba(0,0,0,0));
border-radius: 410px 410px 410px 410px / 310px 310px 410px 410px;
box-shadow: inset 8px -4px 5px -7px rgba(0,0,0,1)
,inset -8px -4px 5px -7px rgba(0,0,0,1)
, 0 -3px 8px -4px rgba(250,250,250,.4)
,inset 0 3px 4px -2px rgba(10,10,10,1)
,inset 0 280px 40px -200px rgba(0,0,0,.2)
,inset 0 -200px 40px -200px rgba(180,180,180,.2);
margin-top: 20px;
}

input:checked + .back {
background-image: -webkit-linear-gradient(90deg, black 30%, transparent 65%)
,-webkit-linear-gradient(180deg, rgba(250,250,250,0) 0%, rgba(250,250,250,.4) 50%, rgba(150,150,150,0) 100%);
box-shadow: 28px 28px 28px -28px rgba(0,0,0,.1)
,-28px 28px 28px -22px rgba(0,0,0,.1)
,0 30px 30px 0px rgba(0,0,0,.2)
,inset 0 1px 2px 0 rgba(0,0,0,.6);
padding: 2px 4px;
}

.l,.r {
margin: 0 auto;
text-align: center;
}

.round,#onoff,.back,.but,.on,.off {
user-select: none;
}

用css实现一个心型按钮的形状:

love

这种效果也是可以通过html和css来实现的, 我们先来看看html部分的代码:

1
2
3
4
5
6
7
8
9
<div class="love">
<input id="switch" type="checkbox">
<label class="love-heart" for="switch">
<i class="left"></i>
<i class="right"></i>
<i class="bottom"></i>
<div class="round"></div>
</label>
</div>

来看看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
.love-heart:before,#switch {
display: none;
}

.love-heart, .love-heart::after {
border-color: hsl(231deg 28% 86%);
border: 1px solid;
border-top-left-radius: 100px;
border-top-right-radius: 100px;
width: 10px;
height: 8px;
border-bottom: 0
}

.round {
position: absolute;
z-index: 1;
width: 8px;
height: 8px;
background: hsl(0deg 0% 100%);
box-shadow: rgb(0 0 0 / 24%) 0px 0px 4px 0px;
border-radius: 100%;
left: 0px;
bottom: -1px;
transition: all .5s ease;
animation: check-animation2 .5s forwards;
}

input:checked + label .round {
transform: translate(0px, 0px);
animation: check-animation .5s forwards;
background-color: hsl(0deg 0% 100%);
}

@keyframes check-animation {
0% {
transform: translate(0px, 0px);
}

50% {
transform: translate(0px, 7px);
}

100% {
transform: translate(7px, 7px);
}
}

@keyframes check-animation2 {
0% {
transform: translate(7px, 7px);
}

50% {
transform: translate(0px, 7px);
}

100% {
transform: translate(0px, 0px);
}
}

.love-heart {
box-sizing: border-box;
position: relative;
transform: rotate(-45deg) translate(-50%, -33px) scale(4);
display: block;
border-color: hsl(231deg 28% 86%);
cursor: pointer;
top: 0;
}

input:checked + .love-heart, input:checked + .love-heart::after, input:checked + .love-heart .bottom {
border-color: hsl(347deg 81% 61%);
box-shadow: inset 6px -5px 0px 2px hsl(347deg 99% 72%);
}

.love-heart::after, .love-heart .bottom {
content: "";
display: block;
box-sizing: border-box;
position: absolute;
border-color: hsl(231deg 28% 86%);
}

.love-heart::after {
right: -9px;
transform: rotate(90deg);
top: 7px;
}

.love-heart .bottom {
width: 11px;
height: 11px;
border-left: 1px solid;
border-bottom: 1px solid;
border-color: hsl(231deg 28% 86%);
left: -1px;
top: 5px;
border-radius: 0px 0px 0px 5px;
}

非常的美妙? 不是吗?

用css实现一个input输入框的效果:

input

先看看html的结构:

1
2
3
4
5
6
<div class="form-control">
<input type="value" required="">
<label>
<span style="transition-delay:0ms">U</span><span style="transition-delay:50ms">s</span><span style="transition-delay:100ms">e</span><span style="transition-delay:150ms">r</span><span style="transition-delay:200ms">n</span><span style="transition-delay:250ms">a</span><span style="transition-delay:300ms">m</span><span style="transition-delay:350ms">e</span>
</label>
</div>

在这个结构的基础上, 我们加上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
.form-control {
position: relative;
margin: 20px 0 40px;
width: 190px;
}

.form-control input {
background-color: transparent;
border: 0;
border-bottom: 2px #fff solid;
display: block;
width: 100%;
padding: 15px 0;
font-size: 18px;
color: #fff;
}

.form-control input:focus,
.form-control input:valid {
outline: 0;
border-bottom-color: lightblue;
}

.form-control label {
position: absolute;
top: 15px;
left: 0;
pointer-events: none;
}

.form-control label span {
display: inline-block;
font-size: 18px;
min-width: 5px;
color: #fff;
transition: 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.form-control input:focus+label span,
.form-control input:valid+label span {
color: lightblue;
transform: translateY(-30px);
}

用css实现一个卡片的效果:

card

上代码:

1
2
3
4
5
6
7
8
9
10
11
12
<div class="flip">
<div class="content">
<div class="front">
<h2>Front</h2>
<p>Front Description</p>
</div>
<div class="back">
<h2>Back</h2>
<p>Back Description</p>
</div>
</div>
</div>

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
.flip {
box-shadow: 0 0 10px rgba(128, 128, 128, 0.5);
padding: 1em;
width: 190px;
height: 254px;
transform-style: preserve-3d;
transition: 3s ease;
}

.flip:hover {
transform: rotateY(180deg);
}
/* Content */
.flip .content {
transform-style: preserve-3d;
}

.flip .back, .flip .front {
transform-style: preserve-3d;
backface-visibility: hidden;
display: flex;
flex-direction: column;
}

.flip .back {
transform: rotateY(180deg);
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}

.flip h2,
.flip p {
transform: translateZ(90px);
text-shadow: 0 0 3px black;
text-align: center;
}

.flip h2 {
font-size: 3em;
color: #fff;
letter-spacing: 1px;
}

.flip p {
font-size: 1em;
color: #eee;
line-height: 1.6em;
}

.flip::before,
.flip::after {
content: "";
top: 0;
left: 0;
right: 0;
bottom: 0;
position: absolute;
background-image: linear-gradient(purple, red);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
transform: rotateY(180deg)translateZ(1px);
}

.flip::before {
transform: none;
background-image: linear-gradient(violet, orange);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
}

用css实现一个加载动画:

loading

html部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div aria-label="Orange and tan hamster running in a metal wheel" role="img" class="wheel-and-hamster">
<div class="wheel"></div>
<div class="hamster">
<div class="hamster__body">
<div class="hamster__head">
<div class="hamster__ear"></div>
<div class="hamster__eye"></div>
<div class="hamster__nose"></div>
</div>
<div class="hamster__limb hamster__limb--fr"></div>
<div class="hamster__limb hamster__limb--fl"></div>
<div class="hamster__limb hamster__limb--br"></div>
<div class="hamster__limb hamster__limb--bl"></div>
<div class="hamster__tail"></div>
</div>
</div>
<div class="spoke"></div>
</div>

接下来我们来看看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
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
.wheel-and-hamster {
--dur: 1s;
position: relative;
width: 12em;
height: 12em;
font-size: 14px;
}

.wheel,
.hamster,
.hamster div,
.spoke {
position: absolute;
}

.wheel,
.spoke {
border-radius: 50%;
top: 0;
left: 0;
width: 100%;
height: 100%;
}

.wheel {
background: radial-gradient(100% 100% at center,hsla(0,0%,60%,0) 47.8%,hsl(0,0%,60%) 48%);
z-index: 2;
}

.hamster {
animation: hamster var(--dur) ease-in-out infinite;
top: 50%;
left: calc(50% - 3.5em);
width: 7em;
height: 3.75em;
transform: rotate(4deg) translate(-0.8em,1.85em);
transform-origin: 50% 0;
z-index: 1;
}

.hamster__head {
animation: hamsterHead var(--dur) ease-in-out infinite;
background: hsl(30,90%,55%);
border-radius: 70% 30% 0 100% / 40% 25% 25% 60%;
box-shadow: 0 -0.25em 0 hsl(30,90%,80%) inset,
0.75em -1.55em 0 hsl(30,90%,90%) inset;
top: 0;
left: -2em;
width: 2.75em;
height: 2.5em;
transform-origin: 100% 50%;
}

.hamster__ear {
animation: hamsterEar var(--dur) ease-in-out infinite;
background: hsl(0,90%,85%);
border-radius: 50%;
box-shadow: -0.25em 0 hsl(30,90%,55%) inset;
top: -0.25em;
right: -0.25em;
width: 0.75em;
height: 0.75em;
transform-origin: 50% 75%;
}

.hamster__eye {
animation: hamsterEye var(--dur) linear infinite;
background-color: hsl(0,0%,0%);
border-radius: 50%;
top: 0.375em;
left: 1.25em;
width: 0.5em;
height: 0.5em;
}

.hamster__nose {
background: hsl(0,90%,75%);
border-radius: 35% 65% 85% 15% / 70% 50% 50% 30%;
top: 0.75em;
left: 0;
width: 0.2em;
height: 0.25em;
}

.hamster__body {
animation: hamsterBody var(--dur) ease-in-out infinite;
background: hsl(30,90%,90%);
border-radius: 50% 30% 50% 30% / 15% 60% 40% 40%;
box-shadow: 0.1em 0.75em 0 hsl(30,90%,55%) inset,
0.15em -0.5em 0 hsl(30,90%,80%) inset;
top: 0.25em;
left: 2em;
width: 4.5em;
height: 3em;
transform-origin: 17% 50%;
transform-style: preserve-3d;
}

.hamster__limb--fr,
.hamster__limb--fl {
clip-path: polygon(0 0,100% 0,70% 80%,60% 100%,0% 100%,40% 80%);
top: 2em;
left: 0.5em;
width: 1em;
height: 1.5em;
transform-origin: 50% 0;
}

.hamster__limb--fr {
animation: hamsterFRLimb var(--dur) linear infinite;
background: linear-gradient(hsl(30,90%,80%) 80%,hsl(0,90%,75%) 80%);
transform: rotate(15deg) translateZ(-1px);
}

.hamster__limb--fl {
animation: hamsterFLLimb var(--dur) linear infinite;
background: linear-gradient(hsl(30,90%,90%) 80%,hsl(0,90%,85%) 80%);
transform: rotate(15deg);
}

.hamster__limb--br,
.hamster__limb--bl {
border-radius: 0.75em 0.75em 0 0;
clip-path: polygon(0 0,100% 0,100% 30%,70% 90%,70% 100%,30% 100%,40% 90%,0% 30%);
top: 1em;
left: 2.8em;
width: 1.5em;
height: 2.5em;
transform-origin: 50% 30%;
}

.hamster__limb--br {
animation: hamsterBRLimb var(--dur) linear infinite;
background: linear-gradient(hsl(30,90%,80%) 90%,hsl(0,90%,75%) 90%);
transform: rotate(-25deg) translateZ(-1px);
}

.hamster__limb--bl {
animation: hamsterBLLimb var(--dur) linear infinite;
background: linear-gradient(hsl(30,90%,90%) 90%,hsl(0,90%,85%) 90%);
transform: rotate(-25deg);
}

.hamster__tail {
animation: hamsterTail var(--dur) linear infinite;
background: hsl(0,90%,85%);
border-radius: 0.25em 50% 50% 0.25em;
box-shadow: 0 -0.2em 0 hsl(0,90%,75%) inset;
top: 1.5em;
right: -0.5em;
width: 1em;
height: 0.5em;
transform: rotate(30deg) translateZ(-1px);
transform-origin: 0.25em 0.25em;
}

.spoke {
animation: spoke var(--dur) linear infinite;
background: radial-gradient(100% 100% at center,hsl(0,0%,60%) 4.8%,hsla(0,0%,60%,0) 5%),
linear-gradient(hsla(0,0%,55%,0) 46.9%,hsl(0,0%,65%) 47% 52.9%,hsla(0,0%,65%,0) 53%) 50% 50% / 99% 99% no-repeat;
}

/* Animations */
@keyframes hamster {
from, to {
transform: rotate(4deg) translate(-0.8em,1.85em);
}

50% {
transform: rotate(0) translate(-0.8em,1.85em);
}
}

@keyframes hamsterHead {
from, 25%, 50%, 75%, to {
transform: rotate(0);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(8deg);
}
}

@keyframes hamsterEye {
from, 90%, to {
transform: scaleY(1);
}

95% {
transform: scaleY(0);
}
}

@keyframes hamsterEar {
from, 25%, 50%, 75%, to {
transform: rotate(0);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(12deg);
}
}

@keyframes hamsterBody {
from, 25%, 50%, 75%, to {
transform: rotate(0);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(-2deg);
}
}

@keyframes hamsterFRLimb {
from, 25%, 50%, 75%, to {
transform: rotate(50deg) translateZ(-1px);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(-30deg) translateZ(-1px);
}
}

@keyframes hamsterFLLimb {
from, 25%, 50%, 75%, to {
transform: rotate(-30deg);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(50deg);
}
}

@keyframes hamsterBRLimb {
from, 25%, 50%, 75%, to {
transform: rotate(-60deg) translateZ(-1px);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(20deg) translateZ(-1px);
}
}

@keyframes hamsterBLLimb {
from, 25%, 50%, 75%, to {
transform: rotate(20deg);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(-60deg);
}
}

@keyframes hamsterTail {
from, 25%, 50%, 75%, to {
transform: rotate(30deg) translateZ(-1px);
}

12.5%, 37.5%, 62.5%, 87.5% {
transform: rotate(10deg) translateZ(-1px);
}
}

@keyframes spoke {
from {
transform: rotate(0);
}

to {
transform: rotate(-1turn);
}
}