CSS3的變形transform、過渡transition、動畫animation學(xué)習(xí)
學(xué)習(xí)CSS3動畫animation得先了解一些關(guān)于變形transform、過渡transition的知識
這些新屬性大多在新版瀏覽器得到了支持,有些需要添加瀏覽器前綴(-webkit-、-moz-、-ms-、-o-),本文為簡化內(nèi)容,直接使用了原版屬性
根據(jù)不同屬性的支持度,在實際使用的時候需要添加相應(yīng)的瀏覽器前綴支持
目錄:
旋轉(zhuǎn) div 元素:
div{transform:rotate(7deg);
-ms-transform:rotate(7deg); /* IE 9 */
-moz-transform:rotate(7deg); /* Firefox */
-webkit-transform:rotate(7deg); /* Safari 和 Chrome */
-o-transform:rotate(7deg); /* Opera */
}
把鼠標(biāo)指針放到 div 元素上,其寬度會從 100px 逐漸變?yōu)?300px:
div{width:100px; height:100px;
background:blue; transition:width 2s;
-moz-transition:width 2s; /* Firefox 4 */
-webkit-transition:width 2s; /* Safari and Chrome */
-o-transition:width 2s; /* Opera */
}
div:hover{
width:300px;
}
使用 animation 綁定到一個<div> 元素:
@keyframes mymove
{ from {left:0px;} //起始位置
to {left:200px;} //落點位置
}
@-webkit-keyframes mymove /*Safari and Chrome*/
{ from {left:0px;} to {left:200px;}
}
一、變形transform
變形有rotate旋轉(zhuǎn)、scale縮放、translate位移、skew傾斜、matrix矩陣變形、perspective透視幾種操作,通過例子來了解各個操作
1. 初始頁面結(jié)構(gòu)
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> <style type="text/css"> html { font-family: Arial;
} .box { position: relative; margin: 200px auto; width: 100px; height: 20px; text-align: center; border: 1px solid #ddd; background-color: #75e275; cursor: pointer;
} .left,
.right { position: absolute; top: -10px; width: 10px; height: 40px; background-color: #4d8aeb;
} .left { left: 0;
} .right { right: 0;
} .box:hover { transform: rotate(-30deg);
}
</style> </pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> <div class="box">
<div class="left"></div>
<div class="right"></div>
</div></pre>
2. 變形操作
1)旋轉(zhuǎn) **transform: **rotate(<angle>); angle取值有:角度值deg,弧度值rad,梯度gard,轉(zhuǎn)/圈turn,正數(shù)值代表順時針旋轉(zhuǎn),反之逆時針
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: rotate(-30deg);
}</pre>
如果對元素本身或者元素設(shè)置了perspective值(用于設(shè)置查看者的位置),那么rotate3d()函數(shù)可以實現(xiàn)一個3維空間內(nèi)的旋轉(zhuǎn)
rotateX(angele),相當(dāng)于rotate3d(1,0,0,angle)指定在3維空間內(nèi)的X軸旋轉(zhuǎn)
rotateY(angele),相當(dāng)于rotate3d(0,1,0,angle)指定在3維空間內(nèi)的Y軸旋轉(zhuǎn)
rotateZ(angele),相當(dāng)于rotate3d(0,0,1,angle)指定在3維空間內(nèi)的Z軸旋轉(zhuǎn)
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: perspective(300px) rotateY(120deg);
}</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: rotateY(120deg);
}</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: rotate3d(1, 0, 0, 45deg);
}</pre>
2)縮放 **transform: **scale(<number>[, <number>]); 表示使元素在X軸和Y軸同時縮放
<number>表示縮放倍數(shù),可以是正數(shù),負(fù)數(shù)和小數(shù)。負(fù)數(shù)是先翻轉(zhuǎn)元素然后再縮放。包含兩個參數(shù),如果缺少第二個參數(shù),那么第二個參數(shù)的值等于第一個參數(shù)。
scaleX(<number>):表示只在X軸(水平方向)縮放元素。
scaleY(<number>):表示只在Y軸(垂直方向)縮放元素。
scaleZ(<number>):表示只在Z軸縮放元素。前提是元素本身或者元素的父元素設(shè)定了透視值
同樣的,有scale3d(x, y, z)
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: scale(1.5);
}</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: scale(2, 1);
}</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: scaleY(1.5);
}</pre>
3)位移 **transform: **translate(<translation-value>[, <translation-value>]); 表示使元素在X軸和Y軸同時移動
<translation-value>表示位移量。包含兩個參數(shù),如果省略了第二個參數(shù)則第二個參數(shù)為0;如果參數(shù)為負(fù),則表示往相反的方向移動。
translateX(<translation-value>);表示只在X軸(水平方向)移動元素。
translateY(<translation-value>);表示只在Y軸(垂直方向)移動元素。
translateZ(<translation-value>);表示只在Z軸移動元素,前提是元素本身或者元素的父元素設(shè)定了透視值
同樣的,有transform****(x, y, z)
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: translate(100px);
}</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: translate(-30px, 50px);
}</pre>
4)傾斜 **transform: **skew(<angle> [,<angle>]); 包含兩個參數(shù)值,分別表示X軸和Y軸傾斜的角度,取值類型為角度值deg
如果第二個參數(shù)為空,則默認(rèn)為0,參數(shù)為負(fù)表示向相反方向傾斜。
skewX(<angle>);表示只在X軸(水平方向)傾斜
skewY(<angle>);表示只在Y軸(垂直方向)傾斜
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: skewX(30deg);
}</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: skew(30deg, 30deg);
}</pre>
5)矩陣變形 **transform: **matrix(<number>,<number>,<number>,<number>,<number>,<number>);
matrix()是矩陣函數(shù),以一個含六值的(a,c,e,b,d,f)變換矩陣的形式指定一個2D變換,相當(dāng)于直接應(yīng)用一個[a c e b d f]變換矩陣,其中c和e用正弦或余弦值表示
這六個參數(shù)實際上是一個3*3的矩陣:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform: matrix(1, 0, 0, 2, 40, 20);
}</pre>
同樣的,可用matrix3d定義3D轉(zhuǎn)換,其是一個使用 了16 個值的 4x4 矩陣
6)透視 **transform: perspective(length); 設(shè)置查看者的位置,并將可視內(nèi)容映射到一個視錐上,繼而投影到一個 2D 視平面上
透視還可以直接定義成屬性 perspective: **<length>,但其是設(shè)置所有的子元素有一個共同的透視值
其對于 3D 變換來說至關(guān)重要,如果不指定透視,則 Z 空間中的所有點將平鋪到同一個 2D 視平面中,并且變換結(jié)果中將不存透視深概念。作用于元素的子元素。
如下兩種樣式定義,結(jié)果相同
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">body { perspective: 300px;
} .box:hover { transform: rotateY(30deg);
} .box:hover { transform: perspective(300px) rotateY(30deg);
}</pre>
7) transfrom相關(guān)的其他屬性
除了transform之外,還有一些變換相關(guān)的屬性,這幾個屬性很少用到,還沒理解到位...
- transform-origin 變形原點 -- 允許你改變被轉(zhuǎn)換元素的位置
- transform-style 3D呈現(xiàn) -- 規(guī)定被嵌套元素如何在 3D 空間中顯示
- perspective-origin 透視原點 -- 規(guī)定 3D 元素的底部位置
- backface-visibility 隱藏內(nèi)容的背面 -- 定義元素在不面對屏幕時是否可見
7-1)transform-origin
該屬性提供2個參數(shù)值,第一個用于橫坐標(biāo),第二個用于縱坐標(biāo);如果只提供一個,該值將用于橫坐標(biāo),縱坐標(biāo)將默認(rèn)為50%。
percentage:用百分比指定坐標(biāo)值。可以為負(fù)值。
length:用長度值指定坐標(biāo)值。可以為負(fù)值。
left center right是水平方向取值,而top center bottom是垂直方向的取值。
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">.box:hover { transform-origin: left; transform: rotate(30deg);
}</pre>
7-2) transform-style
設(shè)置內(nèi)嵌的元素在 3D 空間如何呈現(xiàn)。有兩個值:flat:所有子元素在 2D 平面呈現(xiàn);preserve-3d:保留3D空間
7-3) perspective-origin
該屬性定義在X軸和Y軸的3D元素。這個屬性允許你改變3D元素的底部位置。定義時的perspective-origin屬性,它是一個元素的子元素,透視圖,而不是元素本身。
使用此屬性必須和perspective屬性一起使用,只影響3D轉(zhuǎn)換的元素
該屬性提供2個參數(shù)值,第一個用于橫坐標(biāo),第二個用于縱坐標(biāo);如果只提供一個,該值將用于橫坐標(biāo),縱坐標(biāo)將默認(rèn)為50%。
percentage:用百分比指定坐標(biāo)值。可以為負(fù)值。
length:用長度值指定坐標(biāo)值。可以為負(fù)值。
left,center right是水平方向取值,而top center bottom是垂直方向的取值。
7-4)backface-visibility
該屬性可用于隱藏內(nèi)容的背面。默認(rèn)情況下,背面可見,這意味著即使在翻轉(zhuǎn)后,變換的內(nèi)容仍然可見。但當(dāng) backface-visibility 設(shè)置為 hidden 時,旋轉(zhuǎn)后內(nèi)容將隱藏,因為旋轉(zhuǎn)后正面將不再可見。取值有:
visible:默認(rèn)值,旋轉(zhuǎn)的時候背景可見。
hidden:旋轉(zhuǎn)的時候背景不可見。
二、過渡transition
過渡transition是一個復(fù)合屬性,可以同時定義transition-property、transition-duration、transition-timing-function、transition-delay子屬性值
頁面結(jié)構(gòu)如上,根據(jù)例子熟悉這些屬性
1. 綜合transition 可同時設(shè)置四個子屬性值
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> .box { position: relative; margin: 200px auto; width: 100px; height: 20px; text-align: center; border: 1px solid #ddd; background-color: #75e275; cursor: pointer; transition: 2s background-color;
} </pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> .box:hover { background-color: #0f0;
} </pre>
2.transition-property 需要過渡的屬性 all | none | <property>[ ,<property> ]
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">transition-duration: 2s;
transition-property: height,background-color</pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> .box:hover { width: 130px; height: 30px; background-color: #0f0;
} </pre>
- transition-duration設(shè)置動畫過渡的時間[執(zhí)行時間],默認(rèn)值0表示不過渡直接看到執(zhí)行后的結(jié)果。單位是秒s,也可以是毫秒ms
4.transition-delay設(shè)置動畫延遲執(zhí)行的時間,默認(rèn)值0表示立即執(zhí)行,時間可以是正數(shù)也可以是負(fù)數(shù),負(fù)數(shù)表示截斷規(guī)定時間內(nèi)的動畫。單位是秒s,也可以是毫秒ms
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">transition-delay: 1000ms;
transition-duration: 2s;
transition-property: height,background-color</pre>
- transition-timing-function設(shè)置動畫的過渡效果,默認(rèn)值ease,取值有
ease:緩解效果,等同于cubic-bezier(0.25,0.1,0.25,1.0)函數(shù),既立方貝塞爾
linear:線性效果,等同于cubic-bezier(0.0,0.0,1.0,1.0)函數(shù)
ease-in:漸顯效果,等同于cubic-bezier(0.42,0,1.0,1.0)函數(shù)
ease-out:漸隱效果,等同于cubic-bezier(0,0,0.58,1.0)函數(shù)
ease-in-out:漸顯漸隱效果,等同于cubic-bezier(0.42,0,0.58,1.0)函數(shù)
cubic-bezier:特殊的立方貝塞爾曲線效果
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">transition-timing-function: linear;
transition-delay: 1000ms;
transition-duration: 2s;
transition-property: height,background-color</pre>
三、動畫animation
動畫的使用,首先通過@(-webkit-)keyframes 定義動畫名稱及動畫的行為,再通過animation屬性設(shè)置動畫特征相關(guān)值進(jìn)行調(diào)用
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> @keyframes test { from {
width: 100px; height: 20px;
} 50% { height: 50px;
} to { width: 130px; height: 30px; background-color: #0f0;
} }
.box:hover { animation: test 2s;
}</pre>
以上代碼設(shè)置了一個名稱為test的動畫,動畫執(zhí)行時間為2s,定義了從開始(from|0%)到結(jié)束(to|100%)的動畫行為,開始的from可以省略,但結(jié)束的不可省略
見效果圖
1. 綜合animation ,可同時定義多個子屬性
2. animation-name 動畫名稱,需與@keyframes中設(shè)置的一致
3. animation-duration 動畫執(zhí)行時間 <time>:正數(shù),單位可以是秒(s)或者毫秒(ms)。默認(rèn)值為0,表明動畫不執(zhí)行
4. animation-delay 動畫延遲時間 默認(rèn)值0表示立即執(zhí)行,正數(shù)為動畫延遲一定時間,負(fù)數(shù)為截斷一定時間內(nèi)的動畫。單位為秒(s)或毫秒(s)
- animation-timing-function 動畫的過渡類型,取值有:
ease:緩解效果,等同于cubic-bezier(0.25,0.1,0.25,1.0)函數(shù),既立方貝塞爾。
linear:線性效果,等同于cubic-bezier(0.0,0.0,1.0,1.0)函數(shù)。
ease-in:漸顯效果,等同于cubic-bezier(0.42,0,1.0,1.0)函數(shù)。
ease-out:漸隱效果,等同于cubic-bezier(0,0,0.58,1.0)函數(shù)。
ease-in-out:漸顯漸隱效果,等同于cubic-bezier(0.42,0,0.58,1.0)函數(shù)。
step-start:馬上轉(zhuǎn)跳到動畫結(jié)束狀態(tài)。
step-end:保持動畫開始狀態(tài),直到動畫執(zhí)行時間結(jié)束,馬上轉(zhuǎn)跳到動畫結(jié)束狀態(tài)。
steps(<number>[, [ start | end ] ]?):第一個參數(shù)number為指定的間隔數(shù),即把動畫分為n步階段性展示,第二個參數(shù)默認(rèn)為end,設(shè)置最后一步的狀態(tài),start為結(jié)束時的狀態(tài),end為開始時的狀態(tài),若設(shè)置與animation-fill-mode的效果沖突,而以animation-fill-mode的設(shè)置為動畫結(jié)束的狀態(tài)。
cubic-bezier(<number>, <number>, <number>, <number>):特殊的立方貝塞爾曲線效果。
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> @keyframes test { to {
transform: rotate(1turn);
} }
.box:hover { animation-name: test; animation-duration: 2s; animation-delay: -.5s; animation-iteration-count: 2; animation-timing-function: linear;
}</pre>
值得注意的是steps中number參數(shù)的意義, 關(guān)于steps的參數(shù)解析
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> @keyframes test { 50% {
width: 130px;
} 100% { width: 160px;
} }
.box:hover { animation-name: test; animation-duration: 1s; animation-timing-function: steps(5); animation-fill-mode: forwards;
}</pre>
steps(5)表示將動畫行為中的每個間隔分成5段來進(jìn)行,即0-50%分成5段,50%-100%分成5段
6. animation-iteration-count: <number>|infinite; 指定對象動畫循環(huán)播放的次數(shù)。 infinite為無限循環(huán)
7. animation-direction 指定對象動畫運動的方向
normal:正常方向,默認(rèn)。
reverse:動畫反向運行,方向始終與normal相反。(FF14.0.1以下不支持)
alternate:動畫會循環(huán)正反方向交替運動,奇數(shù)次(1、3、5……)會正常運動,偶數(shù)次(2、4、6……)會反向運動,即所有相關(guān)聯(lián)的值都會反向。
alternate-reverse:動畫從反向開始,再正反方向交替運動,運動方向始終與alternate定義的相反。(FF14.0.1以下不支持)
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">animation-direction: alternate-reverse;</pre>
8. animation-fill-mode: 檢索或設(shè)置對象動畫時間之外的狀態(tài),取值有
none:默認(rèn)值。不設(shè)置對象動畫之外的狀態(tài)
forwards:結(jié)束后保持動畫結(jié)束時的狀態(tài),但當(dāng)animation-direction為0,則動畫不執(zhí)行,持續(xù)保持動畫開始時的狀態(tài)
backwards:結(jié)束后返回動畫開始時的狀態(tài)
both:結(jié)束后可遵循forwards和backwards兩個規(guī)則
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> @keyframes test { to {
width: 130px;
} }
.box:hover { animation-name: test; animation-duration: 1s; animation-timing-function: linear; animation-fill-mode: backwards;
} </pre>
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">animation-fill-mode: forwards; /* or both */</pre>
9. animation-play-state: running | paused 檢索或設(shè)置對象動畫的狀態(tài),running為默認(rèn)值
<textarea style="margin: 0px; padding: 0px; border: 1px solid rgb(153, 153, 153); width: 1289px; height: 353.605px; font-family: "Courier New"; font-size: 12px; line-height: 1.5;"></textarea>