一个栗子上手CSS3动画

开发 前端
本篇文章不一一列举CSS3动画的属性,若需要了解API,可前往 MDN 。

[[190930]]

最近杂七杂八的事情很多,很多知识都没来得及总结,是时候总结总结,开启新的篇章~

本篇文章不一一列举CSS3动画的属性,若需要了解API,可前往 MDN 。

在开始栗子前,我们先补补基础知识。

css3动画分类:

  • 补间动画 – 具有连贯性的动画
  • 逐帧动画 – 使用steps过渡方式实现跳跃

animation常用属性及场景:

  1. animation: name duration timing-function delay iteration-count direction; 

1. timing-function属性:

  • ease 规定慢速开始,然后变快,然后慢速结束的过渡效果。
  • ease-in 规定以慢速开始的过渡效果。
  • ease-out 规定以慢速结束的过渡效果。
  • ease-in-out 规定以慢速开始和结束的过渡效果。
  • linear 动画从头到尾的速度是相同的。
  • cubic-bezier(n,n,n,n) 在cubic-bezier函数中自己的值,n取值为0~1
  • steps()

2. delay属性:

用于将动画与其他动画的执行时机错开,将动画落到不同的时间点。这个属性很好用~

动画原则:

  1. 运动一般有个惯性,所以要先快后有一个慢一点的反弹。
  2. 背景若使用多个星星闪烁,错位闪烁

配合JS使用

  1. slide.addEventListener("webkitAnimationEnd"function() { 
  2.  
  3.    console.log('eeee') //动画结束再调用 
  4.  
  5. });  

有些情况我们需要确保动画结束后再进行另外一些交互,可使用该事件监听。

实战演习:

假如我们需要实现一个这样简单的动画:

 

 

仔细观察上面的动画,我们发现,它可以由以下3部分组成:

1. 入场动画——从右往左移动

2. 左右循环移动

3. 逐帧动画

实现方法:

使用3个dom元素,最外层dom实现入场动画,第二层dom实现左右移动,第三层dom实现逐帧动画。

优点:调试方便,节省时间。

缺点:dom多。

1. dom结构

  1. <div class="anima_entrance"
  2.  
  3.     <div class="anima_move"
  4.  
  5.         <div class="anima_sprite"></div> 
  6.  
  7.     </div> 
  8.  
  9. </div>  

2. 分析动画形成的时间轴: 

 

 

 

入场动画持续0.6s,只播放一次,左右移动以及逐帧动画持续2s,循环播放,代码如下:

  1. .anima_entrance { 
  2.  
  3.     animation: anima_entrance .6s ease-in-out both; 
  4.  
  5.  
  6.   
  7.  
  8. .anima_move { 
  9.  
  10.     animation: anima_move 2s linear .6s infinite both; 
  11.  
  12.  
  13.   
  14.  
  15. .anima_sprite { 
  16.  
  17.     animation: anima_sprite 2s step-end .6s infinite both; 
  18.  
  19.  

3. 使用steps()实现逐帧动画:

使用下面这张雪碧图,通过改变background-position实现动画切换。 

 

 

 

蹬蹬蹬,效果如下面所示,是不是很失望😞 

 

 

 

原因:由于animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果是连贯性的。此时可以使用steps()取消补间动画。

贴一个图: 

 

 

 

steps(1,start): 动画一开始就跳到 100% 直到这一帧(不是整个周期)结束 == step-start

steps(1,end): 保持 0% 的样式直到这一帧(不是整个周期)结束 == step-end

接着,我们将timing-function改成 step-end,效果如下:

  1. animation: sprite 2s step-end .6s infinite both;  

 

 

 

出现想要的效果了哈~不错。

完整的css代码如下:

  1.  .anima_entrance { 
  2.     position: absolute
  3.  
  4.     z-index: 3; 
  5.  
  6.     top: 5.1rem; 
  7.  
  8.     left: 4.05rem; 
  9.  
  10.     width: 12.9rem; 
  11.  
  12.     height: 19.1rem; 
  13.  
  14.     -webkit-animation: anima_entrance .6s ease-in-out both; 
  15.  
  16.     animation: anima_entrance .6s ease-in-out both; 
  17.  
  18.  
  19.   
  20.  
  21. .anima_move { 
  22.  
  23.     width: 218px; 
  24.  
  25.     height: 382px; 
  26.  
  27.     position: absolute
  28.  
  29.     z-index: 1; 
  30.  
  31.     top: 0; 
  32.  
  33.     left: 42px; 
  34.  
  35.     -webkit-animation: anima_move 2s linear .6s infinite both; 
  36.  
  37.     animation: anima_move 2s linear .6s infinite both; 
  38.  
  39.  
  40.   
  41.  
  42. .anima_sprite { 
  43.  
  44.     width: 218px; 
  45.  
  46.     height: 382px; 
  47.  
  48.     background: url(demo.png) no-repeat 0 0; 
  49.  
  50.     background-size: 25.8rem 19.1rem; 
  51.  
  52.     -webkit-animation: anima_sprite 2s step-end .6s infinite both; 
  53.  
  54.     animation: anima_sprite 2s step-end .6s infinite both; 
  55.  
  56.  
  57.   
  58.  
  59. @keyframes anima_entrance { 
  60.  
  61.     0% { 
  62.  
  63.         -webkit-transform: translate3d(18.75rem, 0, 0); 
  64.  
  65.         transform: translate3d(18.75rem, 0, 0); 
  66.  
  67.     } 
  68.  
  69.     100% { 
  70.  
  71.         -webkit-transform: translate3d(0, 0, 0); 
  72.  
  73.         transform: translate3d(0, 0, 0); 
  74.  
  75.     } 
  76.  
  77.  
  78.   
  79.  
  80. @keyframes anima_move { 
  81.  
  82.     0%, 16%, 42%, 74%, 100% { 
  83.  
  84.         -webkit-transform: translate3d(0, 0, 0); 
  85.  
  86.         transform: translate3d(0, 0, 0); 
  87.  
  88.     } 
  89.  
  90.     29% { 
  91.  
  92.         -webkit-transform: translate3d(-1rem, 0, 0); 
  93.  
  94.         transform: translate3d(-1rem, 0, 0); 
  95.  
  96.     } 
  97.  
  98.     87% { 
  99.  
  100.         -webkit-transform: translate3d(1rem, 0, 0); 
  101.  
  102.         transform: translate3d(1rem, 0, 0); 
  103.  
  104.     } 
  105.  
  106.  
  107.   
  108.  
  109. @keyframes anima_sprite { 
  110.  
  111.     0%, 16%, 42%, 58%, 74%, 100% { 
  112.  
  113.         background-position: -12.9rem 0; 
  114.  
  115.     } 
  116.  
  117.     8%, 50%, 66% { 
  118.  
  119.         background-position: 0 0; 
  120.  
  121.     } 
  122.  
  123.  
责任编辑:庞桂玉 来源: 前端大全
相关推荐

2011-06-29 13:22:58

CSS3

2012-09-13 09:24:31

CSSJSjQ

2012-06-21 14:20:16

CSS3

2015-05-27 07:44:34

日历控件 jQueryCSS3

2015-04-23 15:40:59

CSS3叮当猫

2014-04-29 10:39:27

CSS3JavaScript

2012-12-26 10:34:56

CSSWeb前端

2012-03-02 10:50:20

jQuery

2015-12-01 09:52:03

CSS3动画源码

2021-03-30 05:58:01

JavascriptCss3转盘小游戏

2013-01-30 16:15:40

adobeHTML5css3

2015-10-14 09:50:11

css3动画模拟

2021-09-08 22:28:13

前端Css3动画

2017-05-03 11:30:20

CSS3小黄人动画

2013-04-10 09:28:24

CSS3CSS

2011-05-11 16:13:43

CSS3

2023-05-09 12:31:48

CSS3动画库场景

2024-03-13 08:21:53

冒泡排序动画

2021-02-24 09:36:03

鸿蒙CSS应用开发

2011-11-24 09:13:16

CSS
点赞
收藏

51CTO技术栈公众号