css3 实现逐帧动画

实现逐帧动画需要使用到的是Animation动画,该CSS3的Animation有八个属性;分别是如下:
1: animation-name
2: animation-duration
3: animation-delay
4: animation-iteration-count
5: animation-direction
6: animation-play-state
7: animation-fill-mode
8: animation-timing-function

含义分别如下:
animation-name 规定需要绑定到选择器的 keyframe 名称
animation-duration 规定完成动画所花费的时间,以秒或毫秒计。
animation-delay 规定在动画开始之前的延迟。
animation-iteration-count 规定动画应该播放的次数。如果设置 infinite 是动画循环播放。
animation-direction 规定是否应该轮流反向播放动画。
animation-play-state 属性规定动画正在运行还是暂停。属性值有2个;paused(规定动画已暂停。) running(规定动画正在播放。)
animation-fill-mode 规定动画在播放之前或之后,其动画效果是否可见。 有如下四个值:
1: none(不改变默认行为);
2: forwards (当动画完成后,保持最后一个属性值(在最后一个关键帧中定义)。)
3. backwards 在animation-delay 所指定的一段时间内,在动画显示之前,应用开始属性值(在第一个关键帧中定义)。
4. both 向前和向后填充模式都被应用。

其中forwards属性值经常会被使用到;

animation-timing-function 规定动画的速度曲线。
至于动画基本的东西这边不讲解,有空大家可以看看如下这篇文章;http://www.cnblogs.com/tugenhua0707/p/5385261.html

我们先来理解如何做css3的逐帧动画,该动画的过程类似于gif动画;

那么需要先理解使用动画函数的属性 steps;
该属性值如下使用:
animation: redBag-heart1 5s steps(1, end) infinite;
-webkit-animation: redBag-heart1 5s steps(1, end) infinite;

该属性值有2个参数:
第一个参数指定了时间函数中的间隔数量(必须是正整数)。
第二个参数可选,接受 start 和 end 两个值,指定在每个间隔的起点或是终点发生阶跃变化,默认为 end。

step-start 等同于steps(1,start),动画分成1步,start 第一帧是第一步动画结束
step-end 等同于steps(1,end):动画分成一步,第一帧是第一步动画开始,默认值为end。

第一个参数是动画分成几步完成,它是指两个关键帧之间的动画,比如如下代码:

@keyframes redBag-heart1 {
0% {
transform: scale(0.08, 0.08);
}
25% {
transform: scale(0.2, 0.2);
}
40% {
transform: scale(0.4, 0.4);
}
60% {
transform: scale(0.6, 0.6);
}
75% {
transform: scale(0.8, 0.8);
}
90% {
transform: scale(1, 1);
}
100% {
transform: scale(3, 3);
}
}

它是指如上的 0% 到 25% 两个关键帧分成几步完成,而不是整个keyframes;因此如果我们设置 steps(1,start)的话,说明是两个间隔分成1步完成;

基本的知识点就是如上;现在我们可以来做一个css3的逐帧动画,无非也就是说0% 它是一个什么样子的,20%的时候又是一张背景图,40%又是一张背景图,这样的组成起来的;所以我们平时看到的很多动画,看起来很复杂,其实都是由很多简单的动作组成起来的;

代码如下:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="@my_programmer">
<title>webkitAnimationEnd</title>
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=no" name="format-detection">
<script src="./js/zepto.js"></script>
<style type="text/css">
body,p,ul,ol,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,form,fieldset,legend,input,select,textarea,button,th,td,blockquote,address,pre{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6,input,textarea,select,button,label{font-size:100%;vertical-align:middle;}
ul,dl,ol{list-style:none;}
img,fieldset{border:none;}
img{display:inline-block;overflow:hidden;vertical-align:top;}
em,address{font-style:normal;}
sup,sub{vertical-align:baseline;}
table{border-collapse:collapse;border-spacing:0;}
q:before{content:"";}
q:after{content:"";}
button{cursor:pointer;}
textarea{word-wrap:break-word;resize:none;} .hidden {display:none;}
/* 撩红包css3动画 */
.animate-container {
width: 150px;
height: 168px;
position: absolute;
top: 0;
left: 0;
overflow: hidden;
}
.redBag-animate {
width: 150px;
height: 168px;
background: url("http://images2015.cnblogs.com/blog/561794/201605/561794-20160529001846147-294383374.png") no-repeat center center;
animation: redBag-animate1 5s steps(1, end) 1;
-webkit-animation: redBag-animate1 5s steps(1, end) 1;
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
}
.redBag-heart {
position: absolute;
top: 0;
left: 0;
z-index: 99;
}
.redBag-heart img {
animation: redBag-heart1 5s steps(1, end) infinite;
-webkit-animation: redBag-heart1 5s steps(1, end) infinite;
margin-top: 31px;
}
@keyframes redBag-heart1 {
0% {
transform: scale(0.08, 0.08);
}
25% {
transform: scale(0.2, 0.2);
}
40% {
transform: scale(0.4, 0.4);
}
60% {
transform: scale(0.6, 0.6);
}
75% {
transform: scale(0.8, 0.8);
}
90% {
transform: scale(1, 1);
}
100% {
transform: scale(3, 3);
}
}
@-webkit-keyframes redBag-heart1 {
0% {
transform: scale(0.08, 0.08);
}
25% {
transform: scale(0.2, 0.2);
}
40% {
transform: scale(0.4, 0.4);
}
60% {
transform: scale(0.6, 0.6);
}
75% {
transform: scale(0.8, 0.8);
}
90% {
transform: scale(1, 1);
}
100% {
transform: scale(3, 3);
}
}
@-webkit-keyframes redBag-animate1 {
0% {
background-position: 0 0;
}
14.3% {
background-position: 0 -168px;
}
28.6% {
background-position: 0 -336px;
}
42.9% {
background-position: 0 -504px;
}
57.2% {
background-position: 0 -672px;
}
71.5% {
background-position: 0 -840px;
}
100% {
background-position: 0 -1008px;
}
}
@keyframes redBag-animate1 {
0% {
background-position: 0 0;
}
14.3% {
background-position: 0 -168px;
}
28.6% {
background-position: 0 -336px;
}
42.9% {
background-position: 0 -504px;
}
57.2% {
background-position: 0 -672px;
}
71.5% {
background-position: 0 -840px;
}
100% {
background-position: 0 -1008px;
}
}
</style>
</head>
<body>
<!-- <div class="btn" id="btn">点击动画</div> -->
<div class="animate-container">
<div class="redBag-animate"></div>
<div class="redBag-heart hidden">
<img src="http://images2015.cnblogs.com/blog/561794/201605/561794-20160529001921991-1838406901.png" width="100%" class="heart-img"/>
</div>
</div> <script type="text/javascript">
var animateContainer = document.querySelector(".redBag-animate");
animateContainer.addEventListener("webkitAnimationEnd", function(e) {
$(".redBag-heart").removeClass("hidden");
}); var animateHeart = document.querySelector(".heart-img");
animateHeart.addEventListener("webkitAnimationEnd", function(e) {
$(".redBag-animate").addClass("hidden");
});
</script>
</body>
</html>

用于检测css动画是否完成的 -- JS代码如下:

var animateContainer = document.querySelector(".redBag-animate");
animateContainer.addEventListener("webkitAnimationEnd", function(e) {
$(".redBag-heart").removeClass("hidden");
}); var animateHeart = document.querySelector(".heart-img");
animateHeart.addEventListener("webkitAnimationEnd", function(e) {
$(".redBag-animate").addClass("hidden");
});

源码下载

css3 实现逐帧动画的更多相关文章

  1. css3 animation实现逐帧动画

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

  2. css3逐帧动画

    写css3动画的时候,我们经常用到animation来实现,默认情况下,animation是属于连贯性的ease动画.我们熟悉的animation动画有ease.ease-in.ease-out.li ...

  3. CSS3 animation属性中的steps实现GIF动图(逐帧动画)

    相信 animation 大家都用过很多,知道是 CSS3做动画用的.而我自己就只会在 X/Y轴 上做位移旋转,使用 animation-timing-function 规定动画的速度曲线,常用到的 ...

  4. animation中的steps()逐帧动画

    在我们平时做宽高确定,需要背景图片切换的效果时,我如果用的是一张大的png图片.而且恰好是所有小图都是从左向右排列的,那么 我们只需测量出某一个小图距左侧有多少像素(x),然后我们banckgroun ...

  5. 利用css3-animation来制作逐帧动画

    前言 趁着还没有元旦之前先码一篇文章,不然到时候估计又被各种虐了,所以趁现在还有力气先来一篇.今天来聊聊css3中的动画属性animation,对这个属性懵懂是在很早的时候有前辈用这个 animati ...

  6. 使用node.js开发一个生成逐帧动画小工具

    在实际工作中我们已经下下来不下于一万个npm包了,像我们熟悉的 vue-cli,react-native-cli 等,只需要输入简单的命令 vue init webpack project,即可快速帮 ...

  7. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  8. Android 逐帧动画

    原理: 逐帧动画是最简单的一种动画.原理就是把几张图片连续显示出来,以达到动画的效果.就相当于下面这种手绘翻页动画啦~ 实现: 1.需要建立一个animation-list来设置静态图片资源.持续时间 ...

  9. 逐帧动画(Frame-by-frame Animations)

    1.这一类动画可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示. xml定义方法 <animation-list xmlns:android=" ...

随机推荐

  1. poj 2528 线段树+离散化

    题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...

  2. [NOIP2012] 提高组 洛谷P1080 国王游戏

    题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍 ...

  3. RCE via XStream object deserialization && SECURITY-247 / CVE-2016-0792 XML reconstruction Object Code Inject

    catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...

  4. cookielib和urllib2模块相结合模拟网站登录

    1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...

  5. AngularJs ngChange、ngChecked、ngClick、ngDblclick

    ngChange 当用户更改输入时,执行给定的表达式.表达式是立即进行执行的,这个和javascript的onChange事件的只有在触发事件的变化结束的时候执行不同. 格式:ng-change=”v ...

  6. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)

    传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...

  7. requst方法简单用一下

    使用getParametar() 获取表单提交过来的文本框的值 setAttribute(String name, Object o)存储此请求中的属性.在请求之间重置属性.此方法常常与 Reques ...

  8. wpf button的mouse(leftbutton)down/up,click事件不响应解决办法

    按照WPF的帮助说明,某些控件的路由事件被内部处理了,已经被标记为Handled,自行定义的事件处理代码便不再起作用了,有时候会很郁闷!         不过WPF提供了必要的方法.         ...

  9. 如何执行一条命令在C#里面。Process

    Download source - 4.15 KB Introduction It is normal practice to open the Windows command prompt and ...

  10. Mysql初始化root密码和允许远程访问

    mysql默认root用户没有密码,输入mysql –u root 进入mysql 1.初始化root密码 进入mysql数据库 1 mysql>update user set password ...