周振林 周振林
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • 学习笔记

    • 《JavaScript教程》
    • 《ES6 教程》
    • 《TypeScript》
    • 《Vue》
    • 《Git》
    • 《小程序笔记》
    • 《JS设计模式总结笔记》
  • 规范
  • Spring
  • 安装教程
  • 其他教程
  • 归真医学
  • 常用药材
  • 学习笔记
  • 经方学习心得
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • 分类
  • 标签
  • 归档

周振林

IT界的小学生
首页
  • 前端文章

    • HTML
    • CSS
    • Tailwind CSS (opens new window)
    • JavaScript
    • Vue3
    • 其他
  • 学习笔记

    • 《JavaScript教程》
    • 《ES6 教程》
    • 《TypeScript》
    • 《Vue》
    • 《Git》
    • 《小程序笔记》
    • 《JS设计模式总结笔记》
  • 规范
  • Spring
  • 安装教程
  • 其他教程
  • 归真医学
  • 常用药材
  • 学习笔记
  • 经方学习心得
  • 基础
  • 虚拟化
  • Docker
  • OpenStack
  • 心情杂货
关于
收藏
  • 分类
  • 标签
  • 归档
  • HTML

  • CSS

    • CSS基础
    • CSS选择器
    • CSS三大特性
    • CSS常用属性
    • CSS盒子模型
    • CSS浮动
    • CSS定位
    • CSS布局
    • Flex布局语法
    • Flex布局实践
    • CSS Grid网格布局教程
    • CSS响应式布局
    • CSS3之transition过渡
    • CSS3之animation动画
      • 动画
      • 变形
      • 旋转
      • 缩放
        • 贝塞尔曲线 cubic-bezier(x1,y1,x2,y2)
    • CSS教程和技巧收藏
  • JavaScript

  • Vue3

  • 学习笔记

  • 其他

  • 前端
  • CSS
周振林
2019-12-25
目录

CSS3之animation动画

# CSS3之animation动画

# 动画

  1. 动画和过渡类似,都是可以实现一些动态的效果,
  2. 不同的是过渡需要在某个属性发生变化时才会触发,动画可以自动触发动态效果
  3. 设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤
animation-name:test;    要对当前元素生效的关键帧的名字
animation-duration:4s;  动画的执行时间
animation-delay: 2s;    动画的延时
animation-timing-function: ease-in-out; 
animation-iteration-count:1; 动画执行的次数 
  可选值:
    次数
    infinite 无限执行

animation-direction:normal; 指定动画运行的方向
  可选值:
    normal 默认值  从 from 向 to运行 每次都是这样 
    reverse 从 to 向 from 运行 每次都是这样 
    alternate 从 from 向 to运行 重复执行动画时反向执行
    alternate-reverse 从 to 向 from运行 重复执行动画时反向执行

animation-play-state:paused; 设置动画的执行状态 
  可选值:
    running 默认值 动画执行
    paused 动画暂停

animation-fill-mode:none; 动画的填充模式
  可选值:
    none 默认值 动画执行完毕元素回到原来位置
    forwards 动画执行完毕元素会停止在动画结束的位置
    backwards 动画延时等待时,元素就会处于开始位置
    both 结合了forwards 和 backwards
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
  • 动画基础案例
<!DOCTYPE html>
<html>
  <div class="box">
    <div class="box1"></div>
  </div>
</html>
<style>
  *{
    margin: 0;
    padding: 0;
  }

  .box{
    width: 800px;
    height: 300px;
    background-color: silver;
    overflow: hidden;
  }

  .box div{
    width: 100px;
    height: 100px;
    margin-bottom: 100px;
    margin-left: 10px;   
  }

  .box1{
    background-color: #bfa;
    animation: test 2s 2 1s alternate;        
  }
  
  .box1:hover{
    animation-play-state: paused;
  }

  @keyframes test {
    /* from表示动画的开始位置 也可以使用 0% */
    from{
        margin-left: 0;
        background-color: orange;
    } 

    /* to动画的结束位置 也可以使用100%*/
    to{
        background-color: red;
        margin-left: 700px;
    }
  }
</style>
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
  • 动画-小球自由落体
<!DOCTYPE html>
<html>
<div class="outer">
  <div class="box1"></div>
  <div class="box2"></div>
  <div class="box3"></div>
  <div class="box4"></div>
  <div class="box5"></div>
  <div class="box6"></div>
  <div class="box7"></div>
  <div class="box8"></div>
  <div class="box9"></div>
</div>
</html>
<style>
  .outer{
    height: 500px;
    border-bottom: 10px black solid;
    margin: 50px auto;
    overflow: hidden;
  }
  .outer div{
    float: left;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background-color: #bfa;
    animation: ball .5s forwards linear infinite alternate;
  }

  div.box2{
    background-color: orange;
    animation-delay: .1s;
  }

  div.box3{
    background-color: yellow;
    animation-delay: .2s;
  }

  div.box4{
    background-color: yellowgreen;
    animation-delay: .3s;
  }

  div.box5{
    background-color: blue;
    animation-delay: .4s;
  }
  div.box6{
    background-color: pink;
    animation-delay: .5s;
  }
  div.box7{
    background-color: tomato;
    animation-delay: .6s;
  }
  div.box8{
    background-color: skyblue;
    animation-delay: .7s;
  }
  div.box9{
    background-color: chocolate;
    animation-delay: .8s;
  }

  /* 创建小球下落的动画 */
  @keyframes ball {
    from{
      margin-top: 0;
    }

    to{
      margin-top: 400px;
    }

    /* 2                                    to{
        margin-top: 400px;
        animation-timing-function: ease-in;
    }

    40%{
        margin-top: 100px;
    }

    80%{
        margin-top: 200px;
    } */

  }
</style>
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

# 变形

  1. 变形就是指通过CSS来改变元素的形状或位置

  2. 变形不会影响到页面的布局

  3. transform 用来设置元素的变形效果

    • 平移:
      transform:translateX() 沿着x轴方向平移
      transform:translateY() 沿着y轴方向平移
      transform:translateZ() 沿着z轴方向平移

    • 平移元素,百分比是相对于自身计算的

    • z轴平移,调整元素在z轴的位置,正常情况就是调整元素和人眼之间的距离,距离越大,元素离人越近

    • z轴平移属于立体效果(近大远小),默认情况下网页是不支持透视,如果需要看见效果必须要设置网页的视距和指定3D舞台,要在变形元素的父元素上

    html{
       /* 设置当前网页的视距为800px,人眼距离网页的距离 */
       perspective: 800px;
     }
     transform-style: preserve-3d;
    
    1
    2
    3
    4
    5
  • Y轴变形
<!DOCTYPE html>
<html>
<body>
  <div class="box1"></div>
  <div class="box2"></div>
</body>
</html>
<style>
  body{
    background-color: rgb(236, 236, 236);
  }
  .box1, .box2{
    float: left;
    width: 150px;
    height: 150px;
    background-color: #fff;          
    margin: 20px 10px;
    transition:all .3s;
  }
  .box1:hover,.box2:hover{
    transform: translateY(-4px);
    box-shadow: 0 0 10px rgba(0, 0, 0, .3)
  }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  • Z轴变形
<!DOCTYPE html>
<html>
<body>
  <div class="box"></div>
</body>
</html>
<style>
  html{
    /* 设置当前网页的视距为800px,人眼距离网页的距离 */
    perspective: 800px;
  }

  body{
    border: 1px red solid;
    background-color: rgb(241, 241, 241);
    transform-style: preserve-3d;
  }
  .box{
    width: 200px;
    height: 200px;
    background-color: #bfa;
    margin: 200px auto;
    transition:2s;
  }

  body:hover .box{  
    transform: translateZ(200px);
  }
</style>
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

# 旋转

  1. 通过旋转可以使元素沿着x y 或 z旋转指定的角度
    transform:rotateX(180deg)
    transform:rotateY()
    transform:rotateZ()
  • 旋转基础案例
<!DOCTYPE html>
<html>
<body>
<div class="box"></div>
</body>
</html>
<style>
  html{
      perspective: 800px;
  }

  body{
      border: 1px red solid;
      background-color: rgb(241, 241, 241);
      transform-style: preserve-3d;
  }
  .box{
      width: 200px;
      height: 200px;
      background-color: #bfa;
      margin: 200px auto;
      transition:2s;
  }

  body:hover .box{
      transform: rotateY(180deg);
      /* 是否显示元素的背面 */
      backface-visibility: hidden;
  }
</style>
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

# 缩放

对元素进行缩放的函数:
transform:scaleX() 水平方向缩放
transform:scaleY() 垂直方向缩放
transform:scale() 双方向的缩放

<html>
 <div class="box"></div>
</html>
<style>
  html{
    perspective:800px;   
  }
  .box{
    width: 100px;
    height: 100px;
    background-color: #bfa;
    transition:2s;
    margin: 100px auto;

    /* 变形的原点 默认值 center*/
    /* transform-origin:20px 20px;  */
  }

  .box:hover{
    transform:scale(2)
  }
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

可用F12开发者工具查看元素及样式,可打开codepen在线编辑代码。

<html>
  <div class="animationBox">
    <div class="rotate">旋转动画1</div>
    <div class="play">
      <div class="img">旋转动画2</div>
      <span><p class="p2"></p></span>
      <span><p></p></span>
      <span><p></p></span>
      <span><p class="p2"></p></span>
    </div>
    <div class="elasticity">弹性动画</div>
    <div class="elasticity2">曲线弹性</div>
  </div>
</html>

<style>
  .animationBox{overflow: hidden;}
  .animationBox>div{
    width: 100px;height: 100px;background: #eee;border-radius: 50%;text-align: center;line-height: 100px;margin: 30px;float:left;
  }
  .rotate{
    animation: rotate 5s linear infinite
  }
  .rotate:hover{ animation-play-state: paused}
  @keyframes rotate {
    0%{transform: rotate(0);}
  100%{transform: rotate(360deg);}
  }
  .animationBox>.play {
    position: relative;
    margin: 50px 30px;
    background:none;
  }
  .play .img{
    position: absolute;
    top: 0;
    left:0;
    z-index: 1;
    width: 100px;height: 100px; background: #eee;
    border-radius: 50%;

    animation: rotate 5s linear infinite
  }
  .play span {
    position: absolute;
    top: 1px;
    left:1px;
    z-index: 0;
    display: block;
    width: 96px;
    height: 96px;
    border: 1px solid #999;
    border-radius: 50%;
  }
  .play span p{display: block;width: 4px;height: 4px;background: #000;margin: -2px 0 0 50%;border-radius: 50%;opacity: 0.5;}
  .play span .p2{margin: 50% 0 0 -2px;}
  .play span{
    animation: wave 5s linear infinite
  }
  .play>span:nth-child(3){
    /* 延迟时间 */
    animation-delay:1s; 
  }
  .play>span:nth-child(4){
    animation-delay:2.2s;
  }
  .play>span:nth-child(5){
    animation-delay:3.8s;
  }
  
  @keyframes wave {
    0%
    {
      transform:scale(1) rotate(360deg);
      opacity: 0.8;
    }
  100%
    {
      transform:scale(1.8) rotate(0deg);
      opacity: 0;
    }
  }


  .elasticity{
    /* 参数说明
      动画名称 花费时间 贝塞尔曲线 延迟开始时间 播放次数n|infinite  是否反向播放动画
    */
    animation: elasticity 1s linear 2s infinite
  }
  
  @keyframes elasticity{
    0%{
      transform: scale(0);
    }
    60%{
      transform: scale(1.1);
    }
    90%{
      transform: scale(1);
    }
  }
  

  .elasticity2{
    /**
    贝塞尔曲线 cubic-bezier(x1,y1,x2,y2)

    通过调整贝塞尔曲线可以设置出多种动画效果,比如反弹效果等
    X轴的范围是0~1,Y轴的取值没有规定,但是也不宜过大
    直线:linear,即cubic-bezier(0,0,1,1)

    贝塞尔曲线在线工具:https://cubic-bezier.com/#.17,.67,.83,.67
      */
    animation: elasticity2 1s cubic-bezier(.39,.62,.74,1.39) 2s infinite
  }
  @keyframes elasticity2{
    0%{
      transform: scale(0);
    }
    90%{
      transform: scale(1);
    }
  }
</style>
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

# 贝塞尔曲线 cubic-bezier(x1,y1,x2,y2)

通过调整贝塞尔曲线可以设置出多种动画效果,比如反弹效果等 X轴的范围是0~1,Y轴的取值没有规定,但是也不宜过大。 如:直线linear,即cubic-bezier(0,0,1,1)

贝塞尔曲线在线工具:https://cubic-bezier.com/#.17,.67,.83,.67 (opens new window)

参考:https://www.w3school.com.cn/css3/index.asp (opens new window)

Last Updated: 2024/10/28, 16:28:52
CSS3之transition过渡
CSS教程和技巧收藏

← CSS3之transition过渡 CSS教程和技巧收藏→

最近更新
01
Docker安装
06-10
02
Docker运行JAR
06-10
03
Docker部署MySQL
06-10
更多文章>
Copyright © 2019-2025 鲁ICP备19032096号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式