Css3 3d animation Step1---介绍如何实现css动画
2012-07-24 17:13
786 查看
转载地址:http://www.iteye.com/topic/1116417
前言:
好久没写文章了,今天看到webQQ又一次升级。我用的chrome的画面切换甚至用到了css 3d animation+3dtransform。
这是本系列的第一篇文章,用于介绍仅用css实现3d动画。也许这篇文章也许过于超前,因为大部分的浏览器器,甚至包括FF,都还没有实现3d transform。涉及到animation的部分,你可以通过webkit内核的浏览器(Chrome or Safari)或者Firefox来浏览,但是涉及到3d transform的东西请用webkit的浏览器器浏览。
Css3 @KeyFrames Rule
什么是KeyFrames?直译过来就是关键帧。你可以把他理解为flash中的关键帧。他以关键字from开始,to结束。中间的部分都为百分比。意为动画到了某个百分比时,他的状态。而帧与帧之间的补间动画浏览器会帮你自动生成。
例如:
Html代码
<style type="text/css">
@keyframes myfirst {
from {background: red;}
to {background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */ {
from {background: red;}
to {background: yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */ {
from {background: red;}
to {background: yellow;}
}
</style>
为了支持将来可能有的标准,你不得不写3遍。
试着运行下面的代码,里面可能会有你没见过的属性,我们下面会讲到
大家等待了两秒后,突然从画面左上角出现一个方块,花费5秒从红色变到黄色。可能大家已经猜到了
Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
@keyframes myfirst {
from {background: red;}
to {background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */ {
from {background: red;}
to {background: yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */ {
from {background: red;}
to {background: yellow;}
}
#sample {
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
/* Firefox: */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-delay: 2s;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
-moz-animation-play-state: running;
/* Safari and Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-delay: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-webkit-animation-play-state: running;
}
</style>
</head>
<body>
<div id="sample" style="width:120px; height:120px;">
</div>
</body>
</html>
animation-name: 和上面的@keyframe的名字对应起来,代表当前用的是何种变换
animation-duration: 代表动画开始到结束总共耗时多少时间
animation-timing-function: 代表动画的进行是均匀的还是先快后慢还是?他有这几个枚举值:linear:线性,ease:默认值,先慢后快,到了快一半的时候再变慢,ease-in:先慢后快,ease-out:先快后慢。ease-in-out:两头慢,中间快。cubic-bezier(n,n,n,n):自定义,n只能输入0-1之间的值。输入的值越大,移动的越慢,4个n代表了动画的4个阶段25%,50%,75%,100%。
也就是说,linear,ease等值,只是cubic-bezier在浏览器中的预设值,可以简单表示如下:
animation-delay:代表动画延迟多少时间开始,默认为0
Html代码
#linear {animation-timing-function: cubic-bezier(0,0,1,1;}
#ease {animation-timing-function: cubic-bezier(0.25,0.1,0.25,1);}
#ease-in {animation-timing-function: cubic-bezier(0.42,0,1,1);}
#ease-out {animation-timing-function: cubic-bezier(0,0,0.58,1);}
#ease-in-out {animation-timing-function: cubic-bezier(0.42,0,0.58,1);}
animation-iteration-count:代表动画执行多少次,默认为1.如果输入infinite,则代表无限次
animation-direction:默认值为normal,代表动画执行完后从头开始。如果设置为alternate,那么动画在结束后,第二遍会反着执行一遍
animation-play-state:默认值running,代表现在是执行状态。如果要让动画停止,只需要把这个值赋值为paused
好,接下来把上面的代码串联起来。
Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
@keyframes myfirst {
from {left:0px; top:0px; background: red; }
50% {left:100px; top:80px;}
70%, 80% {transform: rotate(-360deg);left:240px; top:240px;}
to {left:279px; top:279px; background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */ {
from {left:0px; top:0px; background: red; }
50% {left:100px; top:80px;}
70%, 80% {-moz-transform: rotate(-360deg);left:240px; top:240px;}
to {left:279px; top:279px; background: yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */ {
from {left:0px; top:0px; background: red; }
50% {left:100px; top:80px;}
70%, 80% {-webkit-transform: rotate(-360deg) scale(0.5, 0.5);left:240px; top:240px;}
to {left:279px; top:279px; background: yellow;}
}
#sample {
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
/* Firefox: */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
/* Safari and Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
}
</style>
</head>
<body>
<div style="width:400px; height:400px; border-style:dashed; border-width:1px; position:relative;">
<div id="sample" style="width:120px; height:120px; position:absolute;"></div>
</div>
</body>
</html>
前言:
好久没写文章了,今天看到webQQ又一次升级。我用的chrome的画面切换甚至用到了css 3d animation+3dtransform。
这是本系列的第一篇文章,用于介绍仅用css实现3d动画。也许这篇文章也许过于超前,因为大部分的浏览器器,甚至包括FF,都还没有实现3d transform。涉及到animation的部分,你可以通过webkit内核的浏览器(Chrome or Safari)或者Firefox来浏览,但是涉及到3d transform的东西请用webkit的浏览器器浏览。
Css3 @KeyFrames Rule
什么是KeyFrames?直译过来就是关键帧。你可以把他理解为flash中的关键帧。他以关键字from开始,to结束。中间的部分都为百分比。意为动画到了某个百分比时,他的状态。而帧与帧之间的补间动画浏览器会帮你自动生成。
例如:
Html代码
<style type="text/css">
@keyframes myfirst {
from {background: red;}
to {background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */ {
from {background: red;}
to {background: yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */ {
from {background: red;}
to {background: yellow;}
}
</style>
为了支持将来可能有的标准,你不得不写3遍。
试着运行下面的代码,里面可能会有你没见过的属性,我们下面会讲到
大家等待了两秒后,突然从画面左上角出现一个方块,花费5秒从红色变到黄色。可能大家已经猜到了
Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
@keyframes myfirst {
from {background: red;}
to {background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */ {
from {background: red;}
to {background: yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */ {
from {background: red;}
to {background: yellow;}
}
#sample {
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
/* Firefox: */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-delay: 2s;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
-moz-animation-play-state: running;
/* Safari and Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-delay: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-webkit-animation-play-state: running;
}
</style>
</head>
<body>
<div id="sample" style="width:120px; height:120px;">
</div>
</body>
</html>
animation-name: 和上面的@keyframe的名字对应起来,代表当前用的是何种变换
animation-duration: 代表动画开始到结束总共耗时多少时间
animation-timing-function: 代表动画的进行是均匀的还是先快后慢还是?他有这几个枚举值:linear:线性,ease:默认值,先慢后快,到了快一半的时候再变慢,ease-in:先慢后快,ease-out:先快后慢。ease-in-out:两头慢,中间快。cubic-bezier(n,n,n,n):自定义,n只能输入0-1之间的值。输入的值越大,移动的越慢,4个n代表了动画的4个阶段25%,50%,75%,100%。
也就是说,linear,ease等值,只是cubic-bezier在浏览器中的预设值,可以简单表示如下:
animation-delay:代表动画延迟多少时间开始,默认为0
Html代码
#linear {animation-timing-function: cubic-bezier(0,0,1,1;}
#ease {animation-timing-function: cubic-bezier(0.25,0.1,0.25,1);}
#ease-in {animation-timing-function: cubic-bezier(0.42,0,1,1);}
#ease-out {animation-timing-function: cubic-bezier(0,0,0.58,1);}
#ease-in-out {animation-timing-function: cubic-bezier(0.42,0,0.58,1);}
animation-iteration-count:代表动画执行多少次,默认为1.如果输入infinite,则代表无限次
animation-direction:默认值为normal,代表动画执行完后从头开始。如果设置为alternate,那么动画在结束后,第二遍会反着执行一遍
animation-play-state:默认值running,代表现在是执行状态。如果要让动画停止,只需要把这个值赋值为paused
好,接下来把上面的代码串联起来。
Html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
@keyframes myfirst {
from {left:0px; top:0px; background: red; }
50% {left:100px; top:80px;}
70%, 80% {transform: rotate(-360deg);left:240px; top:240px;}
to {left:279px; top:279px; background: yellow;}
}
@-moz-keyframes myfirst /* Firefox */ {
from {left:0px; top:0px; background: red; }
50% {left:100px; top:80px;}
70%, 80% {-moz-transform: rotate(-360deg);left:240px; top:240px;}
to {left:279px; top:279px; background: yellow;}
}
@-webkit-keyframes myfirst /* Safari and Chrome */ {
from {left:0px; top:0px; background: red; }
50% {left:100px; top:80px;}
70%, 80% {-webkit-transform: rotate(-360deg) scale(0.5, 0.5);left:240px; top:240px;}
to {left:279px; top:279px; background: yellow;}
}
#sample {
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
/* Firefox: */
-moz-animation-name: myfirst;
-moz-animation-duration: 5s;
-moz-animation-timing-function: linear;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
/* Safari and Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
}
</style>
</head>
<body>
<div style="width:400px; height:400px; border-style:dashed; border-width:1px; position:relative;">
<div id="sample" style="width:120px; height:120px; position:absolute;"></div>
</div>
</body>
</html>
相关文章推荐
- css3 | 中的animation/@keyframes xz动画效果-如何使用纯css实现动画效果? 用法:animation:xz 3s 2 4s alternate; 参数:animati
- 【Android 基础】Animation 动画介绍和实现
- 基于animation.css实现动画旋转特效
- 验证requestAnimationFrame、CSS3以及setinterval实现动画效果的优劣
- CSS3动画animation实现漂浮的云
- 如何使用CSS3实现一个3D商品标签
- 如何使用CSS3实现一个3D泡沫图形
- Android如何实现3D旋转,我只要旋转,而不是旋转动画
- CSS3 animation实现点点点loading动画
- CSS3 animation动画实现轮播图效果
- CSS3动画animation实现云彩向左滚动
- 前端动画大全:css的transition、css3的animation、Jquery的animate、js的setInterval
- 深入理解CSS3 Animation 帧动画(step详解)
- jQuery Animation实现CSS3动画示例介绍
- CSS3 Animation实现加载动画
- Animation 动画介绍和实现
- CSS3实现3D文字动画效果
- css3动画显示实现animation属性
- 用CSS3的animation轻松实现背景动画:漂浮的云
- 如何使用CSS3实现一个平滑的3D文本标题