css3动画(transition)属性探讨

昔人已老 2013-05-28

在webapp引用开发中经常会用到css3动画效果,下面我们就一起探讨一下这个属性的使用。

在哪里定义动画效果?

css3动画一般通过鼠标事件或者说状态定义动画,通常我们可以用CSS中伪类和js中的鼠标事件来定义。js的事件也可以,比如click,focus,mousemove,mouseover,mouseout等等

transition的基本语法:

css3动画通过transition属性和其他css属性(颜色,宽高,变形,位置等等)配合来实现。

transition的语法:

transition:[ transition-property ] || [ transition-duration ] || [ transition-timing-function ] || [ transition-delay ]

当然这是简写,我们也可以完整的写:

transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ]*;
transition-duration : <time> [, <time>]*
transition-timing-function : ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>) [, ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier(<number>, <number>, <number>, <number>)]*
transition-delay : <time> [, <time>]*

1.要变化的属性

transition-property:要变化的属性,比如元素变宽则是width,文字颜色要变色这是color;W3C给出了一个可变换属性的列表:

CSS属性改变的对象

background-color色彩

background-image只是渐变

background-position百分比,长度

border-bottom-color色彩

border-bottom-width长度

border-color色彩

border-left-color色彩

border-left-width长度

border-right-color色彩

border-right-width长度

border-spacing长度

border-top-color色彩

border-top-width长度

border-width长度

bottom百分比,长度

color色彩

crop百分比

font-size百分比,长度

font-weight数字

grid-*数量

height百分比,长度

left百分比,长度

letter-spacing长度

line-height百分比,长度,数字

margin-bottom长度

margin-left长度

margin-right长度

margin-top长度

max-height百分比,长度

max-width百分比,长度

min-height百分比,长度

min-width百分比,长度

opacity数字

outline-color色彩

outline-offset整数

outline-width长度

padding-bottom长度

padding-left长度

padding-right长度

padding-top长度

right百分比,长度

text-indent百分比,长度

text-shadow阴影

top百分比,长度

vertical-align百分比,长度,关键词

visibility可见性

width百分比,长度

word-spacing百分比,长度

z-index正整数

zoom数字

该取值还有个强大的“all”取值,表示上表所有属性;

除了以上属性外,当然还有css3中大放异彩的css3变形,比如放大缩小,旋转斜切,渐变等等。

2.动画时间

transition-duration:动画执行的时间,以秒为单位,比如0.1秒可以写成”0.1s”或者”.1s”,注意后面有个“s”单位。

3.动画执行的计算方式

transition-timing-function:动画执行的计算方式,这里时间函数是令人崩溃的贝塞尔曲线,幸好css3提过了几个取值:

ease:逐渐慢下来,函数等同于贝塞尔曲线(0.25,0.1,0.25,1.0).

linear:线性过度,函数等同于贝塞尔曲线(0.0,0.0,1.0,1.0).

ease-in:由慢到快,函数等同于贝塞尔曲线(0.42,0,1.0,1.0).

ease-out:由快到慢,函数等同于贝塞尔曲线(0,0,0.58,1.0).

ease-in-out:由慢到快在到慢,函数等同于贝塞尔曲线(0.42,0,0.58,1.0)

cubic-bezier:特定的cubic-bezier曲线。(x1,y1,x2,y2)四个值特定于曲线上点P1和点P2。所有值需在[0,1]区域内,否则无效。

4.动画延迟

transition-delay:在动作和变换开始之间等待多久,通常用秒来表示(比如,.1s)。如果你不想延迟,该值可省略。

讲了这么多我们看一个简单例子:

<ul class="test">
	<li>背景色过渡</li>
	<li>背景色过渡</li>
	<li>背景色过渡</li>
	<li>背景色过渡</li>
	<li>背景色过渡</li>
</ul> 

<style> 
.test{}
.test li{background-color:#eee; 
-moz-transition:background-color .5s ease-in;
-webkit-transition:background-color .5s ease-in;
-o-transition:background-color .5s ease-in;
-ms-transition:background-color .5s ease-in;
transition:background-color .5s ease-in;}

.test li:nth-child(1):hover{background-color:#bbb;}  //鼠标滑过背景从#eee变#bbb
.test li:nth-child(2):hover{background-color:#999;}  //鼠标滑过背景从#eee变#999
.test li:nth-child(3):hover{background-color:#630;}  //鼠标滑过背景从#eee变#630
.test li:nth-child(4):hover{background-color:#090;}  //鼠标滑过背景从#eee变#090
.test li:nth-child(5):hover{background-color:#f00;}  //鼠标滑过背景从#eee变#f00
</style>

相关推荐