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 相关文章推荐
如何利用CSS3制作3D效果文字具体实现样式
May 02 HTML / CSS
CSS3新属性transition-property transform box-shadow实例学习
Jun 06 HTML / CSS
纯CSS实现设置半个字符的样式
Jul 03 HTML / CSS
CSS3 实现的加载动画
Dec 07 HTML / CSS
HTML5 placeholder属性详解
Jun 22 HTML / CSS
使用HTML5的链接预取功能(link prefetching)给网站提速
Dec 13 HTML / CSS
详解移动端HTML5页面端去掉input输入框的白色背景和边框(兼容Android和ios)
Dec 15 HTML / CSS
利用Storage Event实现页面间通信的示例代码
Jul 26 HTML / CSS
详解如何解决canvas图片getImageData,toDataURL跨域问题
Sep 17 HTML / CSS
浅谈移动端网页图片预加载方案
Nov 05 HTML / CSS
HTML5 直播疯狂点赞动画实现代码 附源码
Apr 14 HTML / CSS
CSS实现隐藏搜索框功能(动画正反向序列)
Jul 21 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下几个常用的去空、分组、调试数组函数
2009/02/22 PHP
PHP socket 模拟POST 请求实例代码
2016/07/18 PHP
PHP判断文件是否被引入的方法get_included_files用法示例
2016/11/29 PHP
PHP 500报错的快速解决方法
2016/12/14 PHP
thinkPHP5.0框架环境变量配置方法
2017/03/17 PHP
php使用array_chunk函数将一个数组分割成多个数组
2018/12/05 PHP
JS实现随机化快速排序的实例代码
2013/08/01 Javascript
js对象转json数组的简单实现案例
2014/02/28 Javascript
JavaScript替换当前页面的方法
2015/04/03 Javascript
JavaScript中标识符提升问题
2015/06/11 Javascript
jquery实现删除一个元素后面的所有元素功能
2015/12/21 Javascript
ES6中如何使用Set和WeakSet
2016/03/10 Javascript
Angular在一个页面中使用两个ng-app的方法(二)
2017/02/20 Javascript
为Jquery EasyUI 组件加上清除功能的方法(详解)
2017/04/13 jQuery
Vue的elementUI实现自定义主题方法
2018/02/23 Javascript
[43:32]2014 DOTA2华西杯精英邀请赛 5 25 LGD VS NewBee第一场
2014/05/26 DOTA
详解Python编程中基本的数学计算使用
2016/02/04 Python
django批量导入xml数据
2016/10/16 Python
使用sklearn之LabelEncoder将Label标准化的方法
2018/07/11 Python
对pycharm代码整体左移和右移缩进快捷键的介绍
2018/07/16 Python
python实现彩色图转换成灰度图
2019/01/15 Python
详解Python给照片换底色(蓝底换红底)
2019/03/22 Python
Django Sitemap 站点地图的实现方法
2019/04/29 Python
python 基于TCP协议的套接字编程详解
2019/06/29 Python
咖啡为什么会有酸味?你喝到的咖啡為什麼是酸的?
2021/03/17 冲泡冲煮
html5 乒乓球(碰撞检测)实例二
2013/07/25 HTML / CSS
Html5监听手机摇一摇事件的实现
2019/11/07 HTML / CSS
致跳高运动员广播稿
2014/01/13 职场文书
员工考核评语大全
2014/04/26 职场文书
酒店开业策划方案
2014/06/02 职场文书
水利水电专业自荐信
2014/07/08 职场文书
2015年保险公司个人工作总结
2015/05/22 职场文书
领导干部学习三严三实心得体会
2016/01/05 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python
go语言中切片与内存复制 memcpy 的实现操作
2021/04/27 Golang
yyds什么意思?90后已经听不懂00后讲话了……
2022/02/03 杂记