1、过渡

过渡(transition)

  • 通过过渡可以指定一个属性发生变化时的切换方式
  • 通过过渡可以创建一些非常好的效果,提升用户的体验

属性值

transition-property:指定要执行过渡的属性

  • 多个属性间使用,隔开;
  • 如果所有属性都需要过渡,则使用all关键字;
  • 大部分属性都支持过渡效果;
  • 注意过渡时必须是从一个有效数值向另外一个有效数值进行过渡;

transition-duration:指定过渡效果的持续时间

  • 时间单位:s和ms(1s=1000ms)

transition-delay:过渡效果的延迟,等待一段时间后在执行过渡

transition-timing-function:过渡的时序函数

  • linear匀速运动
  • ease 默认值,慢速开始,先加速后减速
  • ease-in 加速运动
  • ease-out 减速运动
  • ease-in-out 先加速后减速
  • cubic-bezier()来指定时序函数 https://cubic-bezier.com
  • steps()分步执行过渡效果,可以设置第二个值:

    • end,在时间结束时执行过渡(默认值)
    • start,在时间开始时执行过渡


    transition:可以同时设置过渡相关的所有属性

  • 只有一个要求,如果要写延迟,则两个时间中第一个是持续时间,第二个是延迟时间

示例

  1. /* transition: margin-left 2s 1s; */
  2. transition-property: margin-left;
  3. transition-duration: 2s;
  4. transition-delay: 1s;

14.过渡与动画 - 图1

几种过渡效果对比

linear匀速运动

  1. transition-timing-function: linear;

14.过渡与动画 - 图2

ease 默认值,慢速开始,先加速后减速

  1. transition-timing-function: ease;

14.过渡与动画 - 图3

ease-in 加速运动

  1. transition-timing-function: ease-in;

14.过渡与动画 - 图4

ease-out 减速运动

  1. transition-timing-function: ease-out;

14.过渡与动画 - 图5

ease-in-out 先加速后减速

  1. transition-timing-function: ease-in-out;

14.过渡与动画 - 图6

cubic-bezier()来指定时序函数

  1. transition-timing-function: cubic-bezier(.17, 1.79, .68, -0.69);

14.过渡与动画 - 图7

steps()分步执行过渡效果

  1. /* transition-timing-function: steps(2, end); */
  2. transition-timing-function: steps(2);

14.过渡与动画 - 图8

  1. transition-timing-function: steps(2, start);

14.过渡与动画 - 图9

2、动画

动画和过渡类似,都是可以实现一些动态的效果,不同的是

  • 过渡需要在某个属性发生变化时才会触发
  • 动画可以自动触发动态效果

设置动画效果,必须先要设置一个关键帧,关键帧设置了动画执行每一个步骤

  1. @keyframes test {
  2. from {
  3. margin-left: 0;
  4. }
  5. to {
  6. margin-left: 900px;
  7. }
  8. }

animation-name 指定动画的关键帧名称

animation-duration:指定动画效果的持续时间

animation-delay:动画效果的延迟,等待一段时间后在执行动画

animation-timing-function:动画的时序函数

animation-iteration-count 动画执行的次数

  • infinite 无限执行

animation-direction 指定动画运行的方向

  • normalfromto运行,每次都是这样,默认值
  • reversetofrom运行,每次都是这样
  • alternatefromto运行,重复执行动画时反向执行
  • alternate-reversetofrom运行,重复执行动画时反向执行

animation-play-state 设置动画的执行状态

  • running 动画执行,默认值
  • paused 动画暂停

animation-fill-mode 动画的填充模式

  • none 动画执行完毕,元素回到原来位置,默认值
  • forwards 动画执行完毕,元素会停止在动画结束的位置
  • backwards 动画延时等待时,元素就会处于开始位置
  • both 结合了forwardsbackwards

示例

  1. /* animation-name: test;
  2. animation-duration: 2s;
  3. animation-delay: 2s;
  4. animation-timing-function: linear;
  5. animation-iteration-count: infinite;
  6. animation-direction: alternate;
  7. animation-fill-mode: both; */
  8. animation: test 2s 2s linear infinite alternate both;

14.过渡与动画 - 图10

3、实战

米兔

  1. .box {
  2. height: 271px;
  3. width: 132px;
  4. background-image: url("/assets/米兔/bigtap-mitu-queue-big.png");
  5. margin: 100px auto;
  6. transition: background-position 1s steps(4);
  7. }
  8. .box:hover {
  9. background-position: -528px 0;
  10. }

14.过渡与动画 - 图11

奔跑的少年

  1. .box {
  2. height: 256px;
  3. width: calc(1536px/6);
  4. background-image: url("/assets/奔跑的少年/bg2.png");
  5. margin: 100px auto;
  6. animation: run 1s steps(6) infinite;
  7. }
  8. /* 关键帧 */
  9. @keyframes run {
  10. from {
  11. background-position: 0 0;
  12. }
  13. to {
  14. background-position: -1536px 0;
  15. }
  16. }

14.过渡与动画 - 图12

弹力球

  1. .outer {
  2. width: 100%;
  3. height: 700px;
  4. border-bottom: 10px solid #000;
  5. /* 外边距重叠,开启BFC */
  6. overflow: hidden;
  7. }
  8. .ball {
  9. width: 100px;
  10. height: 100px;
  11. border-radius: 50%;
  12. background-color: gray;
  13. animation: bounce 6s ease-in;
  14. }
  15. @keyframes bounce {
  16. from {
  17. margin-top: 0;
  18. }
  19. 5%,
  20. 15%,
  21. 25%,
  22. 35%,
  23. 45%,
  24. 55%,
  25. 65%,
  26. 75%,
  27. 85%,
  28. 95%,
  29. 98%,
  30. to {
  31. margin-top: 600px;
  32. animation-timing-function: ease-out;
  33. }
  34. 10%,
  35. 20%,
  36. 30%,
  37. 40%,
  38. 50%,
  39. 60%,
  40. 70%,
  41. 80%,
  42. 90% {
  43. animation-timing-function: ease-in;
  44. }
  45. 10% {
  46. margin-top: 60px;
  47. }
  48. 20% {
  49. margin-top: 120px;
  50. }
  51. 30% {
  52. margin-top: 180px;
  53. }
  54. 40% {
  55. margin-top: 240px;
  56. }
  57. 50% {
  58. margin-top: 300px;
  59. }
  60. 60% {
  61. margin-top: 360px;
  62. }
  63. 70% {
  64. margin-top: 420px;
  65. }
  66. 80% {
  67. margin-top: 480px;
  68. }
  69. 90% {
  70. margin-top: 540px;
  71. }
  72. 96% {
  73. margin-top: 580px;
  74. }
  75. 99% {
  76. margin-top: 590px;
  77. }
  78. }

14.过渡与动画 - 图13

酷炫球

  1. div {
  2. float: left;
  3. width: 100px;
  4. height: 100px;
  5. border-radius: 50%;
  6. animation: bounce .5s infinite ease-in alternate;
  7. }
  8. .ball1 {
  9. background-color: red;
  10. animation-delay: .1s;
  11. }
  12. .ball2 {
  13. background-color: yellow;
  14. animation-delay: .2s;
  15. }
  16. .ball3 {
  17. background-color: green;
  18. animation-delay: .3s;
  19. }
  20. .ball4 {
  21. background-color: blue;
  22. animation-delay: .4s;
  23. }
  24. .ball5 {
  25. background-color: pink;
  26. animation-delay: .5s;
  27. }
  28. .ball6 {
  29. background-color: orange;
  30. animation-delay: .6s;
  31. }
  32. .ball7 {
  33. background-color: fuchsia;
  34. animation-delay: .7s;
  35. }
  36. .ball8 {
  37. background-color: gray;
  38. animation-delay: .8s;
  39. }
  40. .ball9 {
  41. background-color: darkcyan;
  42. animation-delay: .9s;
  43. }
  44. .ball10 {
  45. background-color: indigo;
  46. animation-delay: 1s;
  47. }
  48. .ball11 {
  49. background-color: black;
  50. animation-delay: 1.1s;
  51. }
  52. .ball12 {
  53. background-color: darkcyan;
  54. animation-delay: 1.2s;
  55. }
  56. .ball13 {
  57. background-color: darkkhaki;
  58. animation-delay: 1.3s;
  59. }
  60. .ball14 {
  61. background-color: brown;
  62. animation-delay: 1.4s;
  63. }
  64. .ball15 {
  65. background-color: mediumpurple;
  66. animation-delay: 1.5s;
  67. }
  68. @keyframes bounce {
  69. from {
  70. margin-top: 0;
  71. }
  72. to {
  73. margin-top: 500px;
  74. }
  75. }

14.过渡与动画 - 图14