animation动画(1)

animation关键帧使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@keyframes [动画名字]{
from{
//起始
}
to{
//结束
}
//也可以采用百分比来控制
//始终从0%100%变化,写的顺序不会影响效果,比如:先写100%,再写0%依旧是0~100变化
//当起始或终点没设置时,系统会自动设置初始值为起始或终点状态(未延迟一般情况下)
0%{
//...
}
25%{
//
}
100%{
..
}
}

移动方块小栗子

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}

body {
width: 100vw;
height: 100vh;
background: #34495e;
display: flex;
justify-content: center;
align-items: center;
}

main {
width: 400px;
height: 400px;
border: 1px solid wheat;
}

div {
width: 100px;
height: 100px;
background-color: yellow;
animation-name: hd;
animation-duration: 4s;
}

@keyframes hd {
//可以同时声明关键帧规则
25% {
transform: translateX(300px);
background-color:aqua; //改变背景色
border-radius: 50%;//变成圆
}

50% {
transform: translate(300px, 300px);
border-radius: 0%;//变成方块
}

75% {
transform: translateY(300px);
background-color:tomato;//改变背景色
border-radius: 50%;//再变成圆
}


}
</style>
</head>

<body>
<main>
<div>

</div>
</main>

</body>

</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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}

body {
width: 100vw;
height: 100vh;
background: #34495e;
display: flex;
justify-content: center;
align-items: center;
}

main {
width: 400px;
height: 400px;
border: 1px solid wheat;
}

div {
width: 100px;
height: 100px;
background-color: yellow;
animation-name: hd, bgc, changeshape, size;
animation-duration: 4s, 2s, 2s;
}

@keyframes hd {

25% {
transform: translateX(300px);
}

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

75% {
transform: translateY(300px);
}
}

@keyframes bgc {
25% {
background-color: beige;
}

50% {
background-color: blue;
}

75% {
background-color: violet;
}
}

@keyframes changeshape {
25% {
border-radius: 50%;
}

50% {
border-radius: 0%;
}

75% {
border-radius: 50%;
}
}
</style>
</head>

<body>
<main>
<div>

</div>
</main>

</body>

</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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
main{
width: 100vw;
height: 100vh;
background-color: #34495e;
display: flex;
justify-content: center;
align-items: center;
}
div{
height: 100px;
width: 100px;
border: 2px solid white;

background-color: yellow;
animation-name: scale;
animation-duration: 2s;
}

@keyframes scale {
to{
height: 300px;
width: 300px;
border: 30px solid white; //线慢慢变粗
// border:30px dotted white //瞬间变化
}
}

</style>
</head>
<body>
<main>
<div></div>
</main>

</body>
</html>

动画播放次数

1
2
animation-iteration-count:num|infinite
可以指定次数,或无限循环,若缺省则从头选取补齐

心动

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
main {
height: 100vh;
width: 100vw;
background-color: rgb(11, 35, 116);
display: flex;
justify-content: center;
align-items: center;
}

.heart {
width: 200px;
height: 200px;
background-color: red;
transform: rotate(45deg) scale(.5);
animation-name: hd;
animation-duration: 2s;
animation-iteration-count: infinite;
}

.heart::before {
content: '';
width: 200px;
height: 200px;
border-radius: 50%;
position: absolute;
background-color: red;
transform: translateX(-100px);

}

.heart::after {
content: '';
width: 200px;
height: 200px;
border-radius: 50%;
position: absolute;
background-color: red;
transform: translateY(-100px);
}

@keyframes hd {
25%{
opacity: 1;
transform: rotate(45deg) scale(1);
}
50%{
opacity: .5;
transform: rotate(45deg) scale(.5);
}
75%{
opacity: 1;
transform: rotate(45deg) scale(1);
}
85%{
opacity: .5;
transform: rotate(45deg) scale(.5);
}
to{
opacity: 1;
transform: rotate(45deg) scale(1);
}
}
</style>
</head>

<body>
<main>
<div class="heart"></div>
</main>
</body>

</html>

动画的方向

1
2
3
4
5
animation-direction:变化趋势
normal:从0100,瞬间回到0
reverse:从1000,瞬间回到100
alternate:缓慢从0100再从1000
alternate-reverse:缓慢从1000再从0100

示例

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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<style>
*{
margin:0;
padding:0;
}
main {
height: 100vh;
width: 100vw;
background-color: rgb(11, 35, 116);
display: flex;
justify-content: center;
align-items: center;
}

ul {
list-style: none;
display: flex;
width: 500px;
height: 120px;
}

li {
color: red;
flex: 1;
/* border: solid 1px white; */
display: flex;
justify-content: center;
position: relative;
}

i.fa {
font-size: 3em;
animation-name: hd;
animation-duration: 2s;
animation-iteration-count: infinite;
position: absolute;
}

li:nth-child(1)>i {
animation-direction: normal;
}

li:nth-child(2)>i {
animation-direction: reverse;
}

li:nth-child(3)>i {
animation-direction: alternate;
}

li:nth-child(4)>i {
animation-direction: alternate-reverse;
}

span {
position: absolute;
bottom: 0;
}

@keyframes hd {
to {
transform: scale(3);
}
}
</style>
</head>

<body>
<main>
<ul>
<li>
<i class="fa fa-heart" aria-hidden="true"></i>
<span>normal</span>
</li>
<li>
<i class="fa fa-heart" aria-hidden="true"></i>
<span>reverse</span>
</li>
<li>
<i class="fa fa-heart" aria-hidden="true"></i>
<span>alternate</span>
</li>
<li>
<i class="fa fa-heart" aria-hidden="true"></i>
<span>alternate-reverse</span>
</li>
</ul>
</main>
</body>

</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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
}

main {
height: 100vh;
width: 100vw;
background-color: #34495e;
display: flex;
justify-content: center;
align-items: center;
}

div {
height: 100px;
width: 100px;
background: radial-gradient(at center, #e67e22, #e74c3c);
border-radius: 50%;
animation-name: ball;
animation-duration: 1s;
z-index: 1;
animation-iteration-count: infinite;
animation-direction: alternate-reverse;
}

@keyframes ball {
to {
transform: translateY(-300px);
}
}

section {
width: 300px;
height: 40px;
background: rgba(0, 0, 0, 0.3);
position: absolute;
transform: translateY(50px);
border-radius: 50%;
z-index: 0;
filter: blur(5px);
animation-name: shadow;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-direction: alternate-reverse;
}

@keyframes shadow {
to {
width: 300px;
height: 20px;
filter: blur(35px);
background: rgba(0, 0, 0, 0.1);
}
}
</style>
</head>

<body>
<main>
<div></div>
<section></section>
</main>
</body>

</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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
}

main {
height: 100vh;
width: 100vw;
background-color: #34495e;
display: flex;
justify-content: center;
align-items: center;
}

div {
height: 100px;
width: 100px;
background-color: #f1c40f;
animation-name: scale;
animation-duration: 4s;
animation-delay: 2s;
/*仅仅第一次等两秒*/
animation-iteration-count: infinite;
}

/* 每次都要等两秒 */
/* @keyframes scale {
50%{
transform: scale(1);
}
to{
transform: scale(3);
}
} */

@keyframes scale {
to {
transform: scale(3);
}
}
</style>
</head>

<body>
<main>
<div></div>

</main>
</body>

</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
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="//netdna.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
}

main {
height: 100vh;
width: 100vw;
background-color: #34495e;
display: grid;
grid-template: 1fr/1fr;
}

ul {
display: grid;
grid-template: 1fr/repeat(6, 1fr);
gap: 10px;
list-style: none;

}

li {
background-color: #e67e22;
/* height: 50px; */
animation-name: move;
animation-duration: 3s;
animation-iteration-count: infinite;
}

li:nth-child(1) {
animation-timing-function: ease;
}

li:nth-child(2) {
animation-timing-function: ease-in;
}

li:nth-child(3) {
animation-timing-function: ease-out;
}

li:nth-child(4) {
animation-timing-function: ease-in-out;
}

li:nth-child(5) {
animation-timing-function: linear;
}

li:nth-child(6) {
animation-timing-function: cubic-bezier(1, .01, 1, .65);
}

@keyframes move {
to {
transform: translateY(90vh);
}
}
</style>
</head>

<body>
<main>
<ul>
<li>ease</li>
<li>ease-in</li>
<li>ease-out</li>
<li>ease-in-out</li>
<li>linear</li>
<li>set</li>
</ul>

</main>
</body>

</html>

animation动画(1)
https://blog-theta-ten.vercel.app/2021/07/24/animation动画/
作者
Chen
发布于
2021年7月24日
许可协议