CSS3帧动画

html-css08

CSS3帧动画,第1张

animation动画除了可以实现补间动画外,还可以完成逐帧动画。

在animation的属性中,有个属性 animation-timing-function 一共具有如下这些值

CSS

贝塞尔缓动就是完成补间动画的,比如流畅的缩放、位置移动等动画。

但是逐帧动画没有补间效果的,它不是电脑根据初始和结束状态的差异自动生成中间的过渡帧,而是每一帧都是关键帧,需要自己定义每一帧的状态。

比如像下面这样的图,就只能通过逐帧动画来实现运动效果↓

这几个属性值就是用来处理逐帧动画的:

CSS

steps(number,position) ,有两个参数。

number表示动画的段数。关键帧的数量=段数+1。

position这个比较难理解,有两个值:end和start。end是默认值。

先看一个效果。

两个小球,从0px运动到400px,分为了4个动画步骤,有5个关键帧。第一个是start模式,第二个是end模式。

发现start模式是时间一开始,直接进入第二个关键帧状态,然后顺利顺利走完全程。

end模式有点傻,时间一开始,从第一个关键帧开始跑,结果时间结束了,才走完第四个关键帧,没有完成全部路程就over了。

所以start和end的名字和它所表示的含义刚好相反。

比如执行次数或者填充模式。

为上面的小球加上infinite,可以看出start模式第二次开始的运动都是从第二个关键帧开始的。

加上forwards模式则变得不一样了,forwards是向前的单词意思,但是表示的则是保留动画最后的运动状态,意思和功能也是相反的。

第二个end模式的小球终于有机会走完全程了。

所以,当为end模式设置了forwards的时候要小心,因为它其实多走了一步。

1、这头熊的原始素材一共有8个步骤。

所以使用 steps(8,end) 是最好的方式,因为如果使用 steps(8,start) ,则第一帧不能执行,最后一帧会闪白,图片消失。

因为要一直运动,所以需要加上infinite,当执行完最后一张图的时候,再返回到第一张图,形成一个连贯的完步。

通过背景图片的 background-position 的改变,形成熊的运动。

CSS

可以打开codepen编辑器,把运动时间设置更大一点,可以看到慢动作,一帧一帧是如何显示的。

2、这个logo一共有24张图片

但是logo只运动一次,并且停在结束状态,根据end模式的特征,如果加上forwards的话,会多运动一步。

所以,这里是 steps(23,end) ,为什么是23步,而不是24步,因为forwards模式对它的影响。要想最后一步还要看到图片,那么背景图片的挪动就要少挪动一个图片的宽度。图片整个宽度4800px,每一张图200px的宽高。所以背景图片只需要往左边挪动-4600px即可。

CSS

通过上面的案例可以看出,对象的运动效果由逐帧绘制的图片的间距所影响。间距一样,则速度一样。利用steps不能去改变现成的图片帧与帧之间的速度。

但是利用 step-end 或者 step-start 却是可以的。

3、 step-end 的特别用处

其实 step-end 等价于 steps(1,end) , step-start 等价于 steps(1,start) 。

只有一个步骤的逐帧动画,却可以利用好时间的改变和距离的改变做出速度不同的效果来。

熊从左边快速奔跑出来,一共跑了六步,一步比一步速度放慢。然后在原地踏步。

首先这是一个逐帧动画,但是要实现不同的速度,又没有办法改变原来素材图片与图片之间的距离,所以要使用另外的办法。

第一个完步时间间隔小,left的值间隔大,相当于速度快。背景图片往左移动一张,left的值往右改变一次,熊实现往前奔跑。

CSS

第二个完步的时间间隔稍微大点,left的间隔小了一点,速度变慢了一些。依次类推,速度逐渐放缓。第二个完步又从第一帧开始。

CSS

所以说利用step-end还是可以做出速度不一样的逐帧动画的。

在线制作雪碧图: https://www.toptal.com/developers/css/sprite-generator

HTML代码

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>用CSS3动画给一个小球创建跳跃的动画效果</title>

<link rel="stylesheet" type="text/css" href="index.css" />

</head>

<body>

<section class="main">

<div id="ballWrapper">

<div id="ball"></div>

<div id="ballShadow"></div>

</div>

</section>

</body>

</html>

CSS代码(创建一个与html同目录的index.css文件)

#ballWrapper{position:fixedtop:35%left:50%z-index:100margin-left:-70pxwidth:140pxheight:300pxcursor:pointer-webkit-transition:all 5s linear 0s-moz-transition:all 5s linear 0stransition:all 5s linear 0s-webkit-transform:scale(1)-moz-transform:scale(1)-o-transform:scale(1)transform:scale(1)-ms-transform:scale(1)}

#ballWrapper:active{cursor:pointer-webkit-transform:scale(0)-moz-transform:scale(0)-o-transform:scale(0)transform:scale(0)-ms-transform:scale(0)}

#ball{position:absolutetop:0z-index:11width:140pxheight:140pxborder-radius:70pxbackground:#bbbbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(187,187,187,1)), color-stop(99%,rgba(119,119,119,1))) background: -webkit-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -o-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: -ms-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) background: linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%) box-shadow: inset 0 -5px 15px rgba(255,255,255,0.4), inset -2px -1px 40px rgba(0,0,0,0.4), 0 0 1px #000 cursor: pointer base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2JiYmJiYiIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijk5JSIgc3RvcC1jb2xvcj0iIzc3Nzc3NyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(187,187,187,1) 0,rgba(119,119,119,1) 99%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#bbbbbb', endColorstr='#777777', GradientType=0 )-webkit-animation:jump 1s infinite-moz-animation:jump 1s infinite-o-animation:jump 1s infinite-ms-animation:jump 1s infiniteanimation:jump 1s infinite}

#ball::after{position:absolutetop:10pxleft:30pxz-index:10width:80pxheight:40pxborder-radius:40px/20pxbackground:url(data:image/svg+xml background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(232,232,232,1)), color-stop(1%,rgba(232,232,232,1)), color-stop(100%,rgba(255,255,255,0))) background: -webkit-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -o-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: -ms-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) background: linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%) content: "" base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2U4ZThlOCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjElIiBzdG9wLWNvbG9yPSIjZThlOGU4IiBzdG9wLW9wYWNpdHk9IjEiLz4KICAgIDxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2ZmZmZmZiIgc3RvcC1vcGFjaXR5PSIwIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=)background:-moz-linear-gradient(top,rgba(232,232,232,1) 0,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%)filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#e8e8e8', endColorstr='#00ffffff', GradientType=0 )}

#ballShadow{position:absolutebottom:0left:50%z-index:10margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-transform:scaleY(.3)-moz-transform:scaleY(.3)-o-transform:scaleY(.3)transform:scaleY(.3)-ms-transform:scaleY(.3)-webkit-animation:shrink 1s infinite-moz-animation:shrink 1s infinite-o-animation:shrink 1s infinite-ms-animation:shrink 1s infiniteanimation:shrink 1s infinite}

@-webkit-keyframes jump{0%{top:0-webkit-animation-timing-function:ease-in}

50%{top:140pxheight:140px-webkit-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-webkit-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-webkit-animation-timing-function:ease-out}

95%{top:0-webkit-animation-timing-function:ease-in}

100%{top:0-webkit-animation-timing-function:ease-in}

}

@-moz-keyframes jump{0%{top:0-moz-animation-timing-function:ease-in}

50%{top:140pxheight:140px-moz-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-moz-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-moz-animation-timing-function:ease-out}

95%{top:0-moz-animation-timing-function:ease-in}

100%{top:0-moz-animation-timing-function:ease-in}

}

@-o-keyframes jump{0%{top:0-o-animation-timing-function:ease-in}

50%{top:140pxheight:140px-o-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-o-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-o-animation-timing-function:ease-out}

95%{top:0-o-animation-timing-function:ease-in}

100%{top:0-o-animation-timing-function:ease-in}

}

@-ms-keyframes jump{0%{top:0-ms-animation-timing-function:ease-in}

50%{top:140pxheight:140px-ms-animation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60px-ms-animation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70px-ms-animation-timing-function:ease-out}

95%{top:0-ms-animation-timing-function:ease-in}

100%{top:0-ms-animation-timing-function:ease-in}

}

@keyframes jump{0%{top:0animation-timing-function:ease-in}

50%{top:140pxheight:140pxanimation-timing-function:ease-out}

55%{top:160pxheight:120pxborder-radius:70px/60pxanimation-timing-function:ease-in}

65%{top:120pxheight:140pxborder-radius:70pxanimation-timing-function:ease-out}

95%{top:0animation-timing-function:ease-in}

100%{top:0animation-timing-function:ease-in}

}

@-webkit-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-webkit-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-webkit-animation-timing-function:ease-in}

}

@-moz-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-moz-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-moz-animation-timing-function:ease-in}

}

@-o-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-o-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-o-animation-timing-function:ease-in}

}

@-ms-keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)-ms-animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)-ms-animation-timing-function:ease-in}

}

@keyframes shrink{0%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}

50%{bottom:30pxmargin-left:-10pxwidth:20pxheight:5pxborder-radius:20pxbackground:rgba(20,20,20,.3)box-shadow:0 0 20px 35px rgba(20,20,20,.3)animation-timing-function:ease-out}

100%{bottom:0margin-left:-30pxwidth:60pxheight:75pxborder-radius:30px/40pxbackground:rgba(20,20,20,.1)box-shadow:0 0 20px 35px rgba(20,20,20,.1)animation-timing-function:ease-in}

}