动画

动画示例代码

transition

过度动画

其为众多 <single-transition> 的值缩写。(当两个时间同时出现是,第一个时间为动画长度,第二个时间为动画延时)

  1. transition: <single-transition> [',' <single-transition>]*
  2. <single-transition> = [none | <single-transition-property>] || <time> || <single-transition-timing-function> || <time>
  3. transition: none;
  4. transition: left 2s ease 1s, color 2s;
  5. transition: 2s;
transition-property
  1. transition-property: none | <single-traisition-property> [ ',' <single-transition-property>]*
  2. <single-transition-property> = all | <IDENT>
  3. transition-property: none;
  4. <!-- 默认值为 none -->
  5. transition-property: all;
  6. transition-property: left;
  7. transition-property: left, color;
transition-duration
  1. transition-duration: <time>[, <time>]*
  2. transition-duration: 0s;
  3. transition-duration: 1s;
  4. transition-duration: 1s, 2s, 3s;
transition-delay
  1. transition-delay: <time>[,<time>]*
  2. transition-delay: 0s;
  3. transition-delay: 1s;
  4. transition-delay: 1s, 2s, 3s;
transition-timing-function
  1. transition-timing-function: <single-transition-timing-function>[',' <single-transition-timing-function>]*
  2. <!-- 默认函数为 ease -->
  3. <single-transition-timing-function> = ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier(<number>,<number>,<number>,<number>) | step-start | step-end | steps(<integer>)[, [start | end]]?)
  4. <!-- 对于 cubic-bezier 的曲线,前两个值为 P1 的坐标,后两值为 P2 的坐标 -->
  5. transition-timing-function: ease;
  6. transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
  7. transition-timing-function: linear;
  8. transition-timing-function: cubic-bezier(0, 0, 1, 1);

动画 - 图1

animation

  1. animation: <single-animation> [',' <single-animation>]*
  2. <single-animation> = <single-animation-name> || <time> || <single-animation-timing-function> || <time> || <single-animation-iteration-count> || <single-animation-direction> || <single-animation-fill-mode> || single-animation-play-state>
  3. animation: none;
  4. animation: abc 2s ease 0s 1 normal none running;
  5. animation: abc 2s;
  6. animation: abc 1s 2s both, abcd 2s both;
  7. <!-- 调用多个动画 -->

动画可自动运行,但transition需要触发。

animation-name

animation-name 的名字可自由定义。

  1. animation-name: <single-animation-name>#
  2. <single-animation-name> = none | <IDENT>
  3. animation-name: none;
  4. animation-name: abc;
  5. animation-name: abc, abcd;
animation-duration

transition-duration 属性值类似。

  1. animation-duration: <time>[, <time>]*
  2. animation-duration: 0s;
  3. animation-duration: 1s;
  4. animation-duration: 1s, 2s, 3s;
animation-timing-function

其与之前的 transition-timing-function 完全一模一样。

  1. animation-timing-function: <timing-function>#
  2. <single-timing-function> = <single-transition-timing-function>
  3. animation-timing-function: ease;
  4. animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
  5. animation-timing-function: linear;
  6. animation-timing-function: cubic-bezier(0, 0, 1, 1);
  7. animation-timing-function: ease, linear;
animation-iteration-count

其用于动画执行的次数(其默认值为 1)。

  1. animation-iteration-count: <single-animation-iteration-count>#
  2. <single-animation-iteration-count> = infinite | <number>
  3. animation-iteration-count: 1;
  4. animation-iteration-count: infinite;
  5. animation-iteration-count: 1, 2, infinite;
animation-direction

其用于定义动画的运动方向。

  1. animation-direction:<single-animation-direction>#
  2. <single-animation-direction> = normal | reverse | alternate | alternate-revers
  3. animation-direction: reverse
  4. <!-- 动画相反帧的播放 -->
  5. animation-direction: alternate
  6. <!-- 往返执行动画 -->
  7. animation-direction: alternate-revers
  8. <!-- 相反的往返动画 -->
animation-play-state

其用于设定动画的播放状态。

  1. animation-play-state: <single-animation-play-state>#
  2. <single-animation-play-state> = running | paused
  3. animation-play-state: running;
  4. animation-play-state: pasued;
  5. animation-play-state: running, paused;
animation-delay

其用于设置动画的延时,同 transition-delay 值相同。

  1. animation-delay: <time>[, <time>]*
  2. anim
  3. animation-delay: 0s;
  4. animation-delay: 1s;
  5. animation-delay: 1s, 2s, 3s;
animation-fill-mode

其用于设置动画开始时,是否保持第一帧的动画和动画在结束时时候保持最后的状态。

  1. animation-fill-mode: <single-animation-fill-mode>[',' <single-animation-fill-mode>]*
  2. <single-animation-fill-mode> = none | backwards | forwards | both
  3. animation-fill-mode: none;
  4. <!-- 不做设置 -->
  5. animation-fill-mode: backwards;
  6. <!-- 动画开始时出现在第一帧的状态 -->
  7. animation-fill-mode: forwards;
  8. <!-- 动画结束时保留动画结束时的状态 -->
  9. animation-fill-mode: both;
  10. <!-- 开始和结束时都应保留关键帧定义的状态(通常设定) -->
  11. animation-fill-mode: forwards, backwards;
@keyframes

其用于定义关键帧。

  1. <!-- 写法一 -->
  2. @keyframes abc {
  3. from {opacity: 1; height: 100px;}
  4. to {opacity: 0.5; height: 200px;}
  5. }
  6. <!-- 写法二 -->
  7. @keyframes abcd {
  8. 0% {opacity: 1; height: 100px;}
  9. 100% {opacity: 0.5; height: 200px}
  10. }
  11. @keyframes flash {
  12. 0%, 50%, 100% {opacity: 1;}
  13. 25%, 75% {opacity: 0;}
  14. }
  15. <!-- 例子 -->
  16. animation: abc 0.5s both;
  17. animation: flash 0.5s both;
  18. animaiton: abc 0.5s both, flash 0.5s both;

动画 - 图2