css3 动画animation

动画animationcss3 的动画样式

暂停/开始播放动画:animation-play-state

属性:

描述

animation-name

规定需要绑定到选择器的 keyframe 名称。。

animation-duration

规定完成动画所花费的时间,以秒或毫秒计。

animation-timing-function

规定动画的速度曲线。

animation-delay

规定在动画开始之前的延迟。

animation-iteration-count

规定动画应该播放的次数。

animation-direction

规定是否应该轮流反向播放动画。

调用animation的语法:

object.style.animation="mymove 5s infinite"

//objectDOM树节点对象

节点对象.style.animation = 动画名称 播放的时间长度 循环次数(infinite无限循环

每个属性的基本语法:

Animation-name(动画的名称)

JavaScript 语法:

object.style.animationName="mymove"

animation-name: keyframename|none;

描述

keyframename

规定需要绑定到选择器的 keyframe 的名称。

none

规定无动画效果(可用于覆盖来自级联的动画)。

animation - duration(动画持续播放时间)

JavaScript 语法:

object.style.animationDuration="3s"

animation-duration: time;

描述

time

规定完成动画所花费的时间。默认值是 0,意味着没有动画效果。

animation-timing-function(规定动画的速度曲线)

JavaScript 语法:

object.style.animationTimingFunction="linear"

animation-timing-function: value;

描述

测试

linear

动画从头到尾的速度是相同的。

测试

ease

默认。动画以低速开始,然后加快,在结束前变慢。

测试

ease-in

动画以低速开始。

测试

ease-out

动画以低速结束。

测试

ease-in-out

动画以低速开始和结束。

测试

cubic-bezier(n,n,n,n)

在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。

 

示例:

#div1 {animation-timing-function:linear;}

#div2 {animation-timing-function:ease;}

#div3 {animation-timing-function:ease-in;}

#div4 {animation-timing-function:ease-out;}

#div5 {animation-timing-function:ease-in-out;}

Animation-delay(动画延迟效果)

JavaScript 语法:

object.style.animationDelay="2s"

animation-delay: time;

描述

测试

time

可选。定义动画开始前等待的时间,以秒或毫秒计。默认值是 0。

 

定义和用法

animation-delay 属性定义动画何时开始。

animation-delay 值以秒或毫秒计。

提示:允许负值,-2s 使动画马上开始,但跳过 2 秒进入动画。

 

描述

测试

time

可选。定义动画开始前等待的时间,以秒或毫秒计。默认值是 0。

 

Animation-iteration-count(重复反复次数)

JavaScript 语法:

object.style.animationIterationCount=3

animation-iteration-count: n|infinite;

描述

测试

n

定义动画播放次数的数值。

测试

infinite

规定动画应该无限次播放。

 

Animation-direction(轮流反复播放动画)

定义和用法

animation-direction 属性定义是否应该轮流反向播放动画。

如果 animation-direction 值是 "alternate",则动画会在奇数次数(1、3、5 等等)正常播放,而在偶数次数(2、4、6 等等)向后播放。

注释:如果把动画设置为只播放一次,则该属性没有效果。

 

JavaScript 语法:

object.style.animationDirection="alternate"

animation-direction: normal|alternate;

描述

测试

normal

默认值。动画应该正常播放。

测试

alternate

动画应该轮流反向播放。

 

以上就是动画的基本属性,如何创造一个动画

关键帧:@keyframes

CSS3 @keyframes 规则

如需在 CSS3 中创建动画,您需要学习 @keyframes 规则。

@keyframes 规则用于创建动画。在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果。

 

Internet Explorer 10、Firefox 以及 Opera 支持 @keyframes 规则和 animation 属性。

Chrome 和 Safari 需要前缀 -webkit-。

注释:Internet Explorer 9,以及更早的版本,不支持 @keyframe 规则或 animation 属性。

CSS3 动画

当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果。

通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器:

· 规定动画的名称

· 规定动画的时长

实例

"myfirst" 动画捆绑到 div 元素,时长:5 秒:

div

{

animation: myfirst 5s;

-moz-animation: myfirst 5s; /* Firefox */

-webkit-animation: myfirst 5s; /* Safari 和 Chrome */

-o-animation: myfirst 5s; /* Opera */

}

示例:

@keyframes myfirst

{

from {background: red;}

to {background: yellow;}

}

 

@-moz-keyframes myfirst /* Firefox */

{

from {background: red;}

to {background: yellow;}

}

 

@-webkit-keyframes myfirst /* Safari 和 Chrome */

{

from {background: red;}

to {background: yellow;}

}

 

@-o-keyframes myfirst /* Opera */

{

from {background: red;}

to {background: yellow;}

}

什么是 CSS3 中的动画?

动画是使元素从一种样式逐渐变化为另一种样式的效果。

您可以改变任意多的样式任意多的次数。

请用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%。

0% 是动画的开始,100% 是动画的完成。

为了得到最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。

实例

当动画为 25% 及 50% 时改变背景色,然后当动画 100% 完成时再次改变:

@keyframes myfirst

{

0%   {background: red;}

25%  {background: yellow;}

50%  {background: blue;}

100% {background: green;}

}

 

@-moz-keyframes myfirst /* Firefox */

{

0%   {background: red;}

25%  {background: yellow;}

50%  {background: blue;}

100% {background: green;}

}

 

@-webkit-keyframes myfirst /* Safari 和 Chrome */

{

0%   {background: red;}

25%  {background: yellow;}

50%  {background: blue;}

100% {background: green;}

}

 

@-o-keyframes myfirst /* Opera */

{

0%   {background: red;}

25%  {background: yellow;}

50%  {background: blue;}

100% {background: green;}

}

实例

改变背景色和位置:

@keyframes myfirst

{

0%   {background: red; left:0px; top:0px;}

25%  {background: yellow; left:200px; top:0px;}

50%  {background: blue; left:200px; top:200px;}

75%  {background: green; left:0px; top:200px;}

100% {background: red; left:0px; top:0px;}

}

 

@-moz-keyframes myfirst /* Firefox */

{

0%   {background: red; left:0px; top:0px;}

25%  {background: yellow; left:200px; top:0px;}

50%  {background: blue; left:200px; top:200px;}

75%  {background: green; left:0px; top:200px;}

100% {background: red; left:0px; top:0px;}

}

 

@-webkit-keyframes myfirst /* Safari 和 Chrome */

{

0%   {background: red; left:0px; top:0px;}

25%  {background: yellow; left:200px; top:0px;}

50%  {background: blue; left:200px; top:200px;}

75%  {background: green; left:0px; top:200px;}

100% {background: red; left:0px; top:0px;}

}

 

@-o-keyframes myfirst /* Opera */

{

0%   {background: red; left:0px; top:0px;}

25%  {background: yellow; left:200px; top:0px;}

50%  {background: blue; left:200px; top:200px;}

75%  {background: green; left:0px; top:200px;}

100% {background: red; left:0px; top:0px;}

}

CSS3 动画属性

下面的表格列出了 @keyframes 规则和所有动画属性:

属性

描述

CSS

@keyframes

规定动画。

3

animation

所有动画属性的简写属性,除了 animation-play-state 属性。

3

animation-name

规定 @keyframes 动画的名称。

3

animation-duration

规定动画完成一个周期所花费的秒或毫秒。默认是 0。

3

animation-timing-function

规定动画的速度曲线。默认是 "ease"。

3

animation-delay

规定动画何时开始。默认是 0。

3

animation-iteration-count

规定动画被播放的次数。默认是 1。

3

animation-direction

规定动画是否在下一周期逆向地播放。默认是 "normal"。

3

animation-play-state

规定动画是否正在运行或暂停。默认是 "running"。

3

animation-fill-mode

规定对象动画时间之外的状态。

3

动画暂停和播放

JavaScript 语法:

object.style.animationPlayState="paused"

语法

animation-play-state: paused|running;

描述

测试

paused

规定动画已暂停。

测试

running

规定动画正在播放。

测试

查询网络地址:

http://www.w3school.com.cn/css3/css3_animation.asp

猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/80545494