CSS3中的動畫功能分為Transitions
功能與Animations
功能,這兩種功能都可以通過改變CSS中的屬性值來產(chǎn)生動畫效果。
Transitions
功能支持從一個屬性值平滑過渡到另一個屬性值,Animations
功能支持通過關(guān)鍵幀的指定來在頁面上產(chǎn)生更復(fù)雜的動畫效果。
Transitions功能
Transitions功能的使用方法
Transitions
功能通過將元素的某個屬性從一個屬性值在指定的時間內(nèi)平滑過渡到另一個屬性值來實現(xiàn)動畫功能,可通過transitions
屬性來使用Transitions
功能。
// transition屬性的使用方法
transition:property duration timing-function
其中property
表示對哪個屬性進行平滑過渡,duration
表示在多長時間內(nèi)完成屬性值的平滑過渡, timing-function
表示通過什么方法來進行平滑過渡。
<style type="text/css">
div{
background-color: #ffff00;
transition: background-color 1s linear;
-webkit-transition: background-color 1s linear;
-moz-transition: background-color 1s linear;
-o-transition: background-color 1s linear;
}
div:hover{
background-color: #00ffff;
}
</style>
<div>示例文字</div>
上面的代碼當(dāng)鼠標(biāo)指針移動到div
元素上時,在1秒內(nèi)div
元素的背景色會從黃色平滑過渡到淺藍(lán)色。
在CSS3中,還有另外一種使用Transitions
功能的方法,就是將transitions
屬性中的三個參數(shù)改寫成transitíon-property
屬性、transition-duratíon
屬性、transition-timing-function
屬性,這三個屬性的含義及屬性值的指定方法與transitíons
屬性中的三個參數(shù)的含義及指定方法完全相同。
-webkit-transition-property:background-color;
-webkit-transition-duration:1s;
-webkit-transition-timing-tunction:linear;
-moz-transition-property:background-color;
-moz-transition-duration:1s;
-moz-transition-timing-function:linear;
-o-transition-property:background-color;
-o-transition-duration:18;
-o-transition-timing-function:linear;
使用Transitions功能同時平滑過渡多個屬性值
可以使用Transitions
功能同時對多個屬性值進行平滑過渡。
<style type="text/css">
div{
background-color: #ffff00;
color: #000000;
width: 300px;
transition: background-color 1s linear, color 1s linear, width 1s linear;
-webkit-transition: background-color 1s linear, color 1s linear, width 1s linear;
-moz-transition: background-color 1s linear, color 1s linear, width 1s linear;
-o-transition: background-color 1s linear, color 1s linear, width 1s linear;
}
div:hover{
background-color: #003366;
color: #ffffff;
width: 400px;
}
</style>
<div>示例文字</div>
上面的代碼當(dāng)鼠標(biāo)指針放到div
元素上時,在1秒內(nèi)完成了div
元素的背景色、字體色和寬度這三個屬性值的平滑過渡。
另外,可以通過改變元素的位置屬性值、實現(xiàn)變形處理的transform
屬性值來讓元素實現(xiàn)移動、旋轉(zhuǎn)等動畫效果。
<style type="text/css">
img{
position: absolute;
top: 70px;
left: 0;
transform: rotate(0deg);
transition: left 1s linear, -o-transform 1s linear;
-webkit-transform: rotate(0deg);
-webkit-transition: left 1s linear, -webkit-transform 1s linear;
-moz-transform: rotate(0deg);
-moz-transition: left 1s linear, -moz-transform 1s linear;
-o-transform: rotate(0deg);
-o-transition: left 1s linear, -o-transform 1s linear;
}
div:hover img{
position: absolute;
left: 30px;
transform: rotate(720deg);
-webkit-transform: rotate(720deg);
-moz-transform: rotate(720deg);
-o-transform: rotate(720deg);
}
</style>
<div>
<img src="flower.png" />
</div>
上面的示例中有一個div
元素,div
元素中有一張圖片,當(dāng)鼠標(biāo)指針停留在圖像上時,圖像會向右移動30px
,并且順時針旋轉(zhuǎn)720度。
Animations功能
Animations功能的使用方法
Animations
功能與Transitions
功能相同, 都是通過改變元素的屬性值來實現(xiàn)動畫效果的。區(qū)別在于:使用Transitions
功能時只能通過指定屬性的開始值與結(jié)束值,然后在這兩個屬性值之間進行平滑過渡的方式來實現(xiàn)動畫效果,因此不能實現(xiàn)比較復(fù)雜的動畫效果,而Animations
則通過定義多個關(guān)鍵幀以及定義每個關(guān)鍵幀中元素的屬性值來實現(xiàn)更為復(fù)雜的動畫效果。
<style type="text/css">
div{
background-color: red;
}
@-webkit-keyframes mycolor{
0%{
background-color: red;
}
40%{
background-color: darkblue;
}
70%{
background-color: yellow;
}
100%{
background-color: red;
}
}
@keyframes mycolor{
0%{
background-color: red;
}
40%{
background-color: darkblue;
}
70%{
background-color: yellow;
}
100%{
background-color: red;
}
}
div:hover{
animation-name:mycolor;
animation-duration:5s;
animation-timing-function:linear;
-webkit-animation-name:mycolor;
-webkit-animation-duration:5s;
-webkit-animation-timing-function:linear;
}
</style>
<div>示例文字</div>
使用Animations
功能的時候,如果使用的是Safari或Chrome瀏覽器,會使用如下所示的方法來創(chuàng)建關(guān)鍵幀的集合。
@-webkit-keyframes 關(guān)鍵幀集合名{ 創(chuàng)建關(guān)鍵幀的代碼 }
關(guān)鍵幀的集合創(chuàng)建好之后, 在元素的樣式中使用該關(guān)鍵幀的集合。
div:hover{
animation-name:mycolor;
animation-duration:5s;
animation-timing-function:linear;
-webkit-animation-name:mycolor;
-webkit-animation-duration:5s;
-webkit-animation-timing-function:linear;
}
在animation-name
屬性中指定關(guān)鍵幀集合的名稱;在animation-duratiion
屬性中指定完成整個動畫所花費的時間;在animation-timing-function
屬性中指定實現(xiàn)動畫的方法。
實現(xiàn)多個屬性值同時改變的動畫
如呆要想實現(xiàn)讓多個屬性值同時變化的動畫, 只需在各關(guān)鍵幀中同時指定這些屬性值就可以了。
<style type="text/css">
div{
position: absolute;
background-color: yellow;
top:100px;
width:500px;
}
@-webkit-keyframes mycolor{
0%{
background-color: red;
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
}
40%{
background-color: darkblue;
transform: rotate(30deg);
-webkit-transform: rotate(30deg);
}
70%{
background-color: yellow;
transform: rotate(-30deg);
-webkit-transform: rotate(-30deg);
}
100%{
background-color: red;
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
}
}
@keyframes mycolor{
0%{
background-color: red;
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
}
40%{
background-color: darkblue;
transform: rotate(30deg);
-webkit-transform: rotate(30deg);
}
70%{
background-color: yellow;
transform: rotate(-30deg);
-webkit-transform: rotate(-30deg);
}
100%{
background-color: red;
transform: rotate(0deg);
-webkit-transform: rotate(0deg);
}
}
div:hover{
animation-name: mycolor;
animation-duration: 5s;
animation-timing-function: linear;
-webkit-animation-name: mycolor;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
}
</style>
<div>示例文字</div>
animation的屬性
屬性 | 描述 | 描述 |
---|---|---|
animation | 所有動畫屬性的簡寫屬性 | |
animation-name | 指定要綁定到選擇器的關(guān)鍵幀的名稱 | none |
animation-duration | 規(guī)定動畫完成一個周期所花費的秒或毫秒 | 默認(rèn)0 |
animation-timing-function | 指定動畫將如何完成一個周期 | 默認(rèn)"ease" |
animation-delay | 定義動畫開始前等待的時間 | 可選,默認(rèn)0,以秒或毫秒計 |
animation-iteration-count | 定義動畫應(yīng)該播放多少次 | infinite:指定動畫播放無限次 默認(rèn)1 |
animation-direction | 規(guī)定動畫是否在下一周期逆向地播放 | normal:默認(rèn)值,動畫按正常播放 reverse:動畫反向播放 alternate:動畫在奇數(shù)次正向播放,在偶數(shù)次反向播放 alternate-reverse:動畫在奇數(shù)次反向播放,在偶數(shù)次正向播放 |
animation-fill-mode | 規(guī)定當(dāng)動畫不播放時(當(dāng)動畫完成時,或當(dāng)動畫有一個延遲未開始播放時),要應(yīng)用到元素的樣式 | 默認(rèn)值none |
animation-play-state | 指定動畫是否正在運行或已暫停 | paused:指定暫停動畫 running:默認(rèn)值,指定正在運行的動畫 |
animation簡寫形式:
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
animation-fill-mode的取值
值 | 描述 |
---|---|
none | 默認(rèn)值。動畫在動畫執(zhí)行之前和之后不會應(yīng)用任何樣式到目標(biāo)元素 |
forwards | 在動畫結(jié)束后(由 animation-iteration-count 決定),動畫將應(yīng)用該屬性值 |
backwards | 動畫將應(yīng)用在 animation-delay 定義期間啟動動畫的第一次迭代的關(guān)鍵幀中定義的屬性值。這些都是 from 關(guān)鍵幀中的值(當(dāng) animation-direction 為 "normal" 或 "alternate" 時)或 to 關(guān)鍵幀中的值(當(dāng) animation-direction 為 "reverse" 或 "alternate-reverse" 時) |
both | 動畫遵循forwards和backwards的規(guī)則。也就是說,動畫會在兩個方向上擴展動畫屬性 |
實現(xiàn)動畫的方法
方法 | 屬性值的變化速度 |
---|---|
linear | 在動畫開始時到結(jié)束時以同樣速度進行改變 |
ease-in | 動畫開始時速度很慢,然后速度沿曲線值進行加快 |
ease-out | 動畫開始時速度很快,然后速度沿曲線值進行放慢 |
ease | 動畫開始時速度很慢,然后速度沿曲線值進行加快,然后再沿曲線值放慢 |
ease-in-out | 動畫開始時速度很慢,然后速度沿曲線值進行加快,然后再沿曲線值放慢 |
cubic-bezier(n,n,n,n) | 在 cubic-bezier 函數(shù)中自己的值。可能的值是從 0 到 1 的數(shù)值 |