CSS3 animation属性 与 @keyframes规则

来源:互联网 发布:阿里云 物理专线 编辑:程序博客网 时间:2024/05/20 01:10
http://www.w3school.com.cn/cssref/pr_keyframes.asp
http://www.w3school.com.cn/cssref/pr_animation.asp
http://www.w3school.com.cn/css3/css3_animation.asp

定义
  1. 通过 @keyframes 规则,您能够创建动画。
  2. 原理:将一套 CSS 样式逐渐变化为另一套样式。
  3. 以百分比来规定改变发生的时间,或者通过关键词 "from" 和 "to",等价于 0% 和 100%。
    1. 0% 是动画的开始时间,100% 动画的结束时间。
    2. 为了获得最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。
  4. IE10 以及更早的版本,不支持 @keyframe 规则或 animation 属性。

基本用法:
animation: name duration timing-function delay iteration-count direction fill-mode;
@keyframes name {keyframes-selector {css-styles;}}


animation 参数解读:
-webkit-animation: name duration timing-function delay iteration-count direction fill-mode;
-moz-animation: name duration timing-function delay iteration-count direction fill-mode;
-o-animation: name duration timing-function delay iteration-count direction fill-mode;
animation: name duration timing-function delay iteration-count direction fill-mode;
  1. name:keyframe 名称。(如:mymove)
  2. duration:持续时间。如果不写,持续时长为 0,即不播放动画。(如:1s)
  3. timing-function:速度曲线。
    1. linear:动画从头到尾的速度是相同的。
    2. ease:默认。动画以低速开始,然后加快,在结束前变慢。
    3. ease-in:动画以低速开始。
    4. ease-out:动画以低速结束。
    5. ease-in-out:动画以低速开始和结束。
    6. cubic-bezier(n,n,n,n)
  4. delay:动画开始之前的延迟。(如:5s)
  5. iteration-count:定义动画播放次数的数值
    1. n:自定义
    2. infinite:无限次
  6. direction:动画是否轮流反向播放
    1. normal:默认值。动画应该正常播放。
    2. alternate:动画应该轮流反向播放。
  7. fill-mode:规定对象动画时间之外的状态。

注:下面的代码我声明了将会对所有元素的所有属性执行动画,即使是非动画区域。
  *, *::before, *::after {     will-change: all }
  很显然,上面的这种使用方法会带来一些没有必要的系统资源的占用和性能损耗。 所以,我们应该尽可能在所需要的元素上使用CSS动画。

keyframes 的4个超级属性
  在 @keyframes 里,我们几乎可以使用任何的CSS属性。在如今的大部分浏览器中,你可以放心的使用 position ,  scale ,  rotate ,  opacity 等等属性值。并且这4个属性可以给动画效果带来更高效,更好的性能表现。


实例:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.main {position: absolute;width: 500px;height: 500px;background: #e8e8e8;}.one {width: 100px;height: 100px;position: relative;top: 0;left: 0;background: pink;-webkit-animation: mymove 3s ease 1s infinite alternate Both;-moz-animation: mymove 3s ease 1s infinite alternate Both;-o-animation: mymove 3s ease 1s infinite alternate Both;animation: mymove 3s ease 1s infinite alternate Both;}@-webkit-keyframes mymove {0% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;left: 0;background: red;}25% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 0;left: 300px;background: yellow;}50% {transform: scale3d(2, 2, 2);opacity: 0;width: 200px;height: 200px;top: 300px;left: 300px;background: pink;}75% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 300px;left: 0px;background: deepskyblue;}100% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;background: red;}}@-moz-keyframes mymove {0% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;left: 0;background: red;}25% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 0;left: 300px;background: yellow;}50% {transform: scale3d(2, 2, 2);opacity: 0;width: 200px;height: 200px;top: 300px;left: 300px;background: pink;}75% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 300px;left: 0px;background: deepskyblue;}100% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;background: red;}}@-o-keyframes mymove {0% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;left: 0;background: red;}25% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 0;left: 300px;background: yellow;}50% {transform: scale3d(2, 2, 2);opacity: 0;width: 200px;height: 200px;top: 300px;left: 300px;background: pink;}75% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 300px;left: 0px;background: deepskyblue;}100% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;background: red;}}@keyframes mymove {0% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;left: 0;background: red;}25% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 0;left: 300px;background: yellow;}50% {transform: scale3d(2, 2, 2);opacity: 0;width: 200px;height: 200px;top: 300px;left: 300px;background: pink;}75% {transform: scale3d(1.5, 1.5, 1.5);opacity: 0.5;width: 150px;height: 150px;top: 300px;left: 0px;background: deepskyblue;}100% {transform: scale3d(1, 1, 1);opacity: 1;width: 100px;height: 100px;top: 0;background: red;}}</style></head><body><div class="main"><div class="one"></div></div></body></html>


0 0