CSS3 animation实现逐帧动画效果


Posted in HTML / CSS onJune 02, 2016

css3里面的animation属性非常强大,但是自己用的比较少,最近有次面试就刚好被问到了,趁现在有时间就对animation做一个小总结。同时实现一个逐帧动画的demo作为练习

animation属性一览

因为animation属性比较多,然后在w3c上看有点蛋疼,干脆也做了一份导图,以后想查看,就一目了然了

CSS3 animation实现逐帧动画效果

使用animation实现逐帧动画

熟悉了animation的属性之后,得找个简单的小项目实现下,逐帧动画好有意思,先跑一个满足下自己
思路很简单,就是给元素一个雪碧图的背景,然后添加的帧动画更改background-position,关键代码:

CSS Code复制内容到剪贴板
  1. @keyframes run{   
  2.     from{   
  3.         background-position: 0 0;   
  4.     }   
  5.     to{   
  6.         background-position: -1540px 0 ;   
  7.     }   
  8. }   
  9. div{   
  10.     width:140px;   
  11.     height:140px;   
  12.     backgroundurl(run.png) ;   
  13.     animation-name:run;   
  14.     animation-duration:1s;   
  15.     animation-iteration-count:infinite;   
  16. }   
  17.   

CSS3 animation实现逐帧动画效果

但是跑起来后我们发现,每帧动画之间帧动画都是滑动,并不是我们要的效果,为什么呢?
原来animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果是连贯性的
知道原因就好办了,解决思路就是:

CSS Code复制内容到剪贴板
  1. @keyframes run{   
  2.     0%, 8%{  /*动作一*/  }   
  3.     9.2%, 17.2%{  /*动作二*/  }   
  4.     ...   
  5. }   
  6.   

step1:动作之间停留8帧,0%设置动作一,动作一结束在8%
step2:动作之间过渡1.2帧,9.2%设置动作二,动作二结束在17.2%

完整代码:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>css3逐帧动画</title>  
  6.     <style>  
  7.     @keyframes run{   
  8.     0%, 8%{  background-position: 0 0;  }   
  9.     9.2%, 17.2%{  background-position: -140px 0;  }   
  10.     18.4%, 26.4%{  background-position: -280px 0 ;  }   
  11.     27.6%, 35.6%{  background-position: -420px 0 ;  }   
  12.     36.8%, 44.8%{  background-position: -560px 0 ;  }   
  13.     46%, 54%{  background-position: -700px 0 ;  }   
  14.     55.2%, 63.2%{  background-position: -840px 0 ;  }   
  15.     64.4%, 72.4%{  background-position: -980px 0 ;  }   
  16.     73.6%, 81.6%{  background-position: -1120px 0 ;  }   
  17.     82.8%, 90.8%{  background-position: -1400px 0 ;  }   
  18.     92%, 100%{  background-position: -1540px 0 ;  }   
  19.     }   
  20.     @-webkit-keyframes run{   
  21.     0%, 8%{  background-position: 0 0;  }   
  22.     9.2%, 17.2%{  background-position: -140px 0;  }   
  23.     18.4%, 26.4%{  background-position: -280px 0 ;  }   
  24.     27.6%, 35.6%{  background-position: -420px 0 ;  }   
  25.     36.8%, 44.8%{  background-position: -560px 0 ;  }   
  26.     46%, 54%{  background-position: -700px 0 ;  }   
  27.     55.2%, 63.2%{  background-position: -840px 0 ;  }   
  28.     64.4%, 72.4%{  background-position: -980px 0 ;  }   
  29.     73.6%, 81.6%{  background-position: -1120px 0 ;  }   
  30.     82.8%, 90.8%{  background-position: -1400px 0 ;  }   
  31.     92%, 100%{  background-position: -1540px 0 ;  }   
  32.     }   
  33.     div{   
  34.         width:140px;   
  35.         height:140px;   
  36.         background: url(blog/754767/201606/754767-20160601000042992-1734972084.png) ;   
  37.         animation:run 1s infinite;   
  38.             -webkit-animation:run 1s infinite;   
  39.         animation-fill-mode : backwards;   
  40.             -webkit-animation-fill-mode : backwards;   
  41.     }   
  42.     </style>  
  43. </head>  
  44. <body>  
  45.     <div></div>  
  46. </body>  
  47. </html>  

还有另外一个实现方法,就是利用steps(),就是帧之间的阶跃动画,这个在w3c里面没有写,先贴个图

CSS3 animation实现逐帧动画效果

由上图可知:

steps(1,start):动画一开始就跳到 100% 直到这一帧(不是整个周期)结束
steps(1,end):保持 0% 的样式直到这一帧(不是整个周期)结束

另外也可以直接设置 animation-timing-function:step-start/step-end
step-start效果等同于steps(1,start),step-end效果等同于steps(1,end)

最终效果,因为录制的问题可能有点卡顿,有兴趣的同学可以直接复制代码去跑下:

完整代码:

XML/HTML Code复制内容到剪贴板
  1. <!DOCTYPE html>  
  2.     <html lang="en">  
  3.     <head>  
  4.         <meta charset="UTF-8">  
  5.         <title>css3逐帧动画</title>  
  6.         <style>  
  7.         @keyframes run{   
  8.             0%{   
  9.                 background-position: 0 0;   
  10.             }   
  11.             8.333%{   
  12.                 background-position: -140px 0;   
  13.             }   
  14.             16.666%{   
  15.                 background-position: -280px 0 ;   
  16.             }   
  17.             25.0%{   
  18.                 background-position: -420px 0 ;   
  19.             }   
  20.             33.333%{   
  21.                 background-position: -560px 0 ;   
  22.             }   
  23.             41.666%{   
  24.                 background-position: -700px 0 ;   
  25.             }   
  26.             50.0%{   
  27.                 background-position: -840px 0 ;   
  28.             }   
  29.             58.333%{   
  30.                 background-position: -980px 0 ;   
  31.             }   
  32.             66.666%{   
  33.                 background-position: -1120px 0 ;   
  34.             }   
  35.             75.0%{   
  36.                 background-position: -1260px 0 ;   
  37.             }   
  38.             83.333%{   
  39.                 background-position: -1400px 0 ;   
  40.             }   
  41.             91.666%{   
  42.                 background-position: -1540px 0 ;   
  43.             }   
  44.             100%{   
  45.                 background-position: 0 0 ;   
  46.             }   
  47.         }   
  48.         @-webkit-keyframes run{   
  49.             0%{   
  50.                 background-position: 0 0;   
  51.             }   
  52.             8.333%{   
  53.                 background-position: -140px 0;   
  54.             }   
  55.             16.666%{   
  56.                 background-position: -280px 0 ;   
  57.             }   
  58.             25.0%{   
  59.                 background-position: -420px 0 ;   
  60.             }   
  61.             33.333%{   
  62.                 background-position: -560px 0 ;   
  63.             }   
  64.             41.666%{   
  65.                 background-position: -700px 0 ;   
  66.             }   
  67.             50.0%{   
  68.                 background-position: -840px 0 ;   
  69.             }   
  70.             58.333%{   
  71.                 background-position: -980px 0 ;   
  72.             }   
  73.             66.666%{   
  74.                 background-position: -1120px 0 ;   
  75.             }   
  76.             75.0%{   
  77.                 background-position: -1260px 0 ;   
  78.             }   
  79.             83.333%{   
  80.                 background-position: -1400px 0 ;   
  81.             }   
  82.             91.666%{   
  83.                 background-position: -1540px 0 ;   
  84.             }   
  85.             100%{   
  86.                 background-position: 0 0 ;   
  87.             }   
  88.         }   
  89.         div{   
  90.             width:140px;   
  91.             height:140px;   
  92.             background: url(754767/201606/754767-20160601000042992-1734972084.png) ;   
  93.             animation:run 1s steps(1, start) infinite;   
  94.                 -webkit-animation:run 1s steps(1, start) infinite;   
  95.         }   
  96.         </style>  
  97.     </head>  
  98.     <body>  
  99.         <div></div>  
  100.     </body>  

CSS3 animation实现逐帧动画效果

原文地址:http://www.cnblogs.com/Fengzp/p/5548493.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

HTML / CSS 相关文章推荐
css动画效果之animation的常用样式
Mar 09 HTML / CSS
CSS3 media queries + jQuery实现响应式导航
Sep 30 HTML / CSS
CSS3 伪类选择器 nth-child()说明
Jul 10 HTML / CSS
纯CSS打造(无图像无js)的非常流行的讲话(语音)气泡效果
Dec 28 HTML / CSS
CSS3不透明度实例讲解
Apr 26 HTML / CSS
使用CSS3滤镜的filter:blur属性制作毛玻璃模糊效果的方法
Jul 08 HTML / CSS
5分钟让你掌握css3阴影、倒影、渐变小技巧(小编推荐)
Aug 15 HTML / CSS
CSS3的calc()做响应模式布局的实现方法
Sep 06 HTML / CSS
HTML5 Canvas API中drawImage()方法的使用实例
Mar 25 HTML / CSS
只要五步 就可以用HTML5/CSS3快速制作便签贴特效(图)
Jun 04 HTML / CSS
HTML5拖拽API经典实例详解
Apr 20 HTML / CSS
HTML5 图片悬停放大的实现代码示例
Dec 04 HTML / CSS
利用CSS3的线性渐变linear-gradient制作边框的示例
Jun 02 #HTML / CSS
移动端Web页面的CSS3 flex布局快速上手指南
May 31 #HTML / CSS
CSS3条纹背景制作的实战攻略
May 31 #HTML / CSS
CSS3实现多重边框的方法总结
May 31 #HTML / CSS
结合CSS3的新特性来总结垂直居中的实现方法
May 30 #HTML / CSS
图解CSS3制作圆环形进度条的实例教程
May 26 #HTML / CSS
CSS中的字体大小设置属性总结
May 24 #HTML / CSS
You might like
自己在做项目过程中学到的PHP知识收集
2012/08/20 PHP
PHP得到mssql的存储过程的输出参数功能实现
2012/11/23 PHP
php set_time_limit()函数的使用详解
2013/06/05 PHP
PHP中使用json数据格式定义字面量对象的方法
2014/08/20 PHP
PHP缓存集成库phpFastCache用法
2014/12/15 PHP
PHP面试题之文件目录操作
2015/10/15 PHP
php实现图片上传、剪切功能
2016/05/07 PHP
PHP CURL采集百度搜寻结果图片不显示问题的解决方法
2017/02/03 PHP
Dojo之路:如何利用Dojo实现Drag and Drop效果
2007/04/10 Javascript
ExtJS GTGrid 简单用户管理
2009/07/01 Javascript
js的window.showModalDialog及window.open用法实例分析
2015/01/29 Javascript
jquery实现仿新浪微博带动画效果弹出层代码(可关闭、可拖动)
2015/10/12 Javascript
jQuery中判断对象是否存在的方法汇总
2016/02/24 Javascript
简单讲解jQuery中的子元素过滤选择器
2016/04/18 Javascript
基于JavaScript实现购物网站商品放大镜效果
2016/09/06 Javascript
$.browser.msie 为空或不是对象问题的多种解决方法
2017/03/19 Javascript
详解 vue better-scroll滚动插件排坑
2018/02/08 Javascript
es6 symbol的实现方法示例
2019/04/02 Javascript
微信小程序开发之获取用户手机号码(php接口解密)
2020/05/17 Javascript
Python中Django框架利用url来控制登录的方法
2015/07/25 Python
Python中死锁的形成示例及死锁情况的防止
2016/06/14 Python
用python实现简单EXCEL数据统计的实例
2017/01/24 Python
Python实现调度算法代码详解
2017/12/01 Python
Python 查找list中的某个元素的所有的下标方法
2018/06/27 Python
Python将文本去空格并保存到txt文件中的实例
2018/07/24 Python
Python线程下使用锁的技巧分享
2018/09/13 Python
用Python实现读写锁的示例代码
2018/11/05 Python
Python和Java的语法对比分析语法简洁上python的确完美胜出
2019/05/10 Python
python多线程高级锁condition简单用法示例
2019/11/07 Python
vscode写python时的代码错误提醒和自动格式化的方法
2020/05/07 Python
python 可视化库PyG2Plot的使用
2021/01/21 Python
债务纠纷委托书
2014/08/30 职场文书
干部作风建设工作总结
2014/10/29 职场文书
工程合作意向书范本
2015/05/09 职场文书
《棉鞋里的阳光》教学反思
2016/02/20 职场文书
go语言-在mac下brew升级golang
2021/04/25 Golang