• CSS3提高: CSS3 动画


    动画

    动画(animationCSS3中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果

    相比较过渡,动画可以实现更多变化,更多控制,连续自动播放等效果。

    动画的基本使用

    制作动画分为两步:

    1.先定义动画

    2.再使用(调用)动画

    1. keyframes 定义动画(类似定义类选择器)

    1. @keyframes 动画名称 {
    2. 0%{
    3. width:100px;
    4. }
    5. 100%{
    6. width:200px;
    7. }
    8. }

     动画序列

    0% 是动画的开始100% 是动画的完成。这样的规则就是动画序列。

    @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果

    动画是使元素从一种样式逐渐变化为另一种样式的效果。您可以改变任意多的样式任意多的次数

    请用百分比来规定变化发生的时间,或用关键词 "from" "to",等同于 0% 100%

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. /* from to 等价于 0% 和 100% */
    10. /* @keyframes move {
    11. from {
    12. transform: translate(0, 0);
    13. }
    14. to {
    15. transform: translate(1000px, 0);
    16. }
    17. } */
    18. /* 动画序列 */
    19. /* 1. 可以做多个状态的变化 keyframe 关键帧 */
    20. /* 2. 里面的百分比要是整数 */
    21. /* 3. 里面的百分比就是 总的时间(我们这个案例10s)的划分 25% * 10 = 2.5s */
    22. @keyframes move {
    23. 0%{
    24. transform: translate(0,0)
    25. }
    26. 25%{
    27. transform: translate(1000px,0)
    28. }
    29. 50%{
    30. transform: translate(1000px,500px)
    31. }
    32. 75%{
    33. transform: translate(0,500px)
    34. }
    35. /* 不加100% 那个会不是自己回去的 而是重新开始了 */
    36. 100%{
    37. transform: translate(0,0)
    38. }
    39. }
    40. div {
    41. width: 100px;
    42. height: 100px;
    43. background-color: pink;
    44. animation-name: move;
    45. animation-duration: 10s;
    46. }
    47. </style>
    48. </head>
    49. <body>
    50. <div>
    51. </div>
    52. </body>
    53. </html>

    2. 元素使用动画

    1. div {
    2. width: 200px;
    3. height: 200px;
    4. background-color: aqua;
    5. margin: 100px auto;
    6. /* 调用动画 */
    7. animation-name: 动画名称;
    8. /* 持续时间 */
    9. animation-duration: 持续时间;
    10. }

     动画跟我们2d转换很相似,但功能更加强大,例如打开页面就实现自动移动,2d转换就实现不了

     动画常用属性

    属性

    描述

    @keyframes

    规定动画。

    animation

    所有动画属性的简写属性,除了animation-play-state属性。

    animation-name

    规定@keyframes动画的名称。(必须的)

    animation-duration

    规定动画完成一个周期所花费的秒或毫秒,默认是0。(必须的)

    animation-timing-function

    规定动画的速度曲线,默认是“ease”。

    animation-delay

    规定动画何时开始,默认是0

    animation-iteration-count

    规定动画被播放的次数,默认是1,还有infinite

    animation-direction

    规定动画是否在下一周期逆向播放,默认是“normal,alternate逆播放

    animation-play-state

    规定动画是否正在运行或暂停。默认是"running",还有"paused"

    animation-fill-mode

    规定动画结束后状态,保持forwards回到起始backwards

    动画简写属性

    animation:动画名称 持续时间 运动曲线  何时开始  播放次数  是否反方向  动画起始或者结束的状态;

    animation: myfirst 5s linear 2s infinite alternate; 

    1. /* animation: name duration timing-function delay iteration-count direction fill-mode; */
    2. animation: move 2s linear 0s 1 alternate forwards;

     这代码效果跟上面的是一样的,但是要注意的顺序还有影响的 例如持续时间和何时开始 都是时间,那么写在前面的肯定为持续时间,前面俩个属性一定要写 name duration

     简写属性里面不包含 animation-play-state

    暂停动画:animation-play-state:   puased;   经常和鼠标经过等其他配合使用

    想要动画走回来 ,而不是直接跳回来:animation-direction   :  alternate

    盒子动画结束后,停在结束位置:  animation-fill-mode  :   forwards

    案例:热点图案例

    就是放4个div

     这个的原因是3个pulse叠在一起了,所有要用绝对定位,不占位置 

    布局效果

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <style>
    9. body{
    10. background: #333;
    11. }
    12. .map{
    13. width: 747px;
    14. height: 617px;
    15. background: url(media/map.png) no-repeat;
    16. margin: 0 auto;
    17. position: relative;
    18. }
    19. .city{
    20. top: 227px;
    21. right: 193px;
    22. position: absolute;
    23. color: #fff;
    24. }
    25. .dotted{
    26. border-radius: 50%;
    27. width: 8px;
    28. height: 8px;
    29. background-color: #09f;
    30. }
    31. .city div[class^="pulse"]{
    32. /* 保证小波纹在父元素里面水平垂直居中 放大之后向四周发散 */
    33. position: absolute;
    34. width: 8px;
    35. height: 8px;
    36. top: 50%;
    37. left: 50%;
    38. transform: translate(-50%,-50%);
    39. box-shadow: 0 0 12px #009dfd;
    40. border-radius: 50%;
    41. }
    42. </style>
    43. </head>
    44. <body>
    45. <div class="map">
    46. <div class="city">
    47. <!-- 小点 -->
    48. <div class="dotted"></div>
    49. <!-- 波纹 -->
    50. <div class="pulse1"></div>
    51. <div class="pulse2"></div>
    52. <div class="pulse3"></div>
    53. </div>
    54. </div>
    55. </body>
    56. </html>

     pulse2被划掉了,这是因为权重不一样 

     完整代码

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <style>
    9. body{
    10. background: #333;
    11. }
    12. .map{
    13. width: 747px;
    14. height: 617px;
    15. background: url(media/map.png) no-repeat;
    16. margin: 0 auto;
    17. position: relative;
    18. }
    19. .city{
    20. top: 227px;
    21. right: 193px;
    22. position: absolute;
    23. color: #fff;
    24. }
    25. .tb{
    26. top: 500px;
    27. right: 80px;
    28. }
    29. .gz{
    30. top: 532px;
    31. right: 173px;
    32. }
    33. .dotted{
    34. border-radius: 50%;
    35. width: 8px;
    36. height: 8px;
    37. background-color: #09f;
    38. }
    39. .city div[class^="pulse"]{
    40. /* 保证小波纹在父元素里面水平垂直居中 放大之后向四周发散 */
    41. position: absolute;
    42. width: 8px;
    43. height: 8px;
    44. top: 50%;
    45. left: 50%;
    46. transform: translate(-50%,-50%);
    47. box-shadow: 0 0 12px #009dfd;
    48. border-radius: 50%;
    49. animation: pulse 1.2s linear infinite;
    50. }
    51. .pulse2{
    52. animation-delay: 0.4s !important;
    53. }
    54. .pulse3{
    55. animation-delay: 0.8s !important;
    56. }
    57. @keyframes pulse{
    58. 0%{
    59. }
    60. 70%{
    61. /* transform: scale(); 我们不要用这个 这会导致阴影也会变大 */
    62. width: 40px;
    63. height: 40px;
    64. opacity: 1;
    65. }
    66. 100%{
    67. width: 70px;
    68. height: 70px;
    69. opacity: 0;
    70. }
    71. }
    72. </style>
    73. </head>
    74. <body>
    75. <div class="map">
    76. <div class="city">
    77. <!-- 小点 -->
    78. <div class="dotted"></div>
    79. <!-- 波纹 -->
    80. <div class="pulse1"></div>
    81. <div class="pulse2"></div>
    82. <div class="pulse3"></div>
    83. </div>
    84. <div class="city tb">
    85. <!-- 小点 -->
    86. <div class="dotted"></div>
    87. <!-- 波纹 -->
    88. <div class="pulse1"></div>
    89. <div class="pulse2"></div>
    90. <div class="pulse3"></div>
    91. </div>
    92. <div class="city gz">
    93. <!-- 小点 -->
    94. <div class="dotted"></div>
    95. <!-- 波纹 -->
    96. <div class="pulse1"></div>
    97. <div class="pulse2"></div>
    98. <div class="pulse3"></div>
    99. </div>
    100. </div>
    101. </body>
    102. </html>

    速度曲线细节

    animation-timing-function规定动画的速度曲线,默认是“ease

    描述

    linear

    动画从头到尾的速度是相同的。匀速

    ease

    默认。动画以低速开始,然后加快,在结束前变慢。

    ease-in

    动画以低速开始。

    ease-out

    动画以低速结束。

    ease-in-out

    动画以低速开始和结束。

    steps()

    指定了时间函数中的间隔数量(步长)

    steps()就是你要用几步来完成你的动画

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. div {
    10. overflow: hidden;
    11. font-size: 20px;
    12. width: 0;
    13. height: 30px;
    14. background-color: pink;
    15. /* 让我们的文字强制一行内显示 */
    16. white-space: nowrap;
    17. /* steps 就是分几步来完成我们的动画 有了steps 就不要在写 ease 或者linear 了 */
    18. animation: w 4s steps(10) forwards;
    19. }
    20. @keyframes w {
    21. 0% {
    22. width: 0;
    23. }
    24. 100% {
    25. width: 200px;
    26. }
    27. }
    28. </style>
    29. </head>
    30. <body>
    31. <div>世纪佳缘我在这里等你</div>
    32. </body>
    33. </html>

     

      案例:奔跑的熊大

    感觉是关键帧动画

     图片的大小为1600px---100px 所以我们设置了一个div200px-100px刚好挡住了

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. body {
    10. background-color: #ccc;
    11. }
    12. div{
    13. position: absolute;
    14. width: 200px;
    15. height: 100px;
    16. background: url(media/bear.png) no-repeat;
    17. /* 我们元素可以添加多个动画,用逗号分各 */
    18. animation: bear 1s steps(8) infinite,move 3s forwards;
    19. }
    20. @keyframes bear{
    21. 0%{
    22. background-position: 0 0;
    23. }
    24. 100%{
    25. background-position: -1600px 0;
    26. }
    27. }
    28. @keyframes move{
    29. 0%{
    30. left: 0;
    31. }
    32. 100%{
    33. /* margin-left: -100px; */
    34. transform: translate(-50%);
    35. left: 50%;
    36. }
    37. }
    38. </style>
    39. </head>
    40. <body>
    41. <div></div>
    42. </body>
    43. </html>

  • 相关阅读:
    即时通讯开发如何设计一个百万级的消息推送
    第二篇 渲染框架2.x
    基于JAVA开发使用IDEA兼容Eclipse的动漫视屏网站
    72道Java线程面试题,一题一答案,不搞花里胡哨
    Hadoop分布式文件系统——HDFS
    FPGA设计-HDMI1接口设计开发
    第2章 Java基础
    【AI副业指南】用AI做心理测试图文号,单月稳赚7000+(附详细教程)
    开发仿抖音APP遇到的问题和解决方案
    <C++> 模板-上
  • 原文地址:https://blog.csdn.net/weixin_64612659/article/details/127638534