標籤:樣本 區分 旋轉 func 支援 .com off 平滑 瞭解
CSS3中和動畫有關的屬性有三個 transform、 transition 和 animation。下面來一一說明: transform 從字面來看transform的釋義為改變,使…變形;轉換 。這裡我們就可以理解為變形。那都能怎麼變呢? none 表示不進行變換; rotate 旋轉 transform:rotate(20deg) 旋轉角度可以為負數。需要先有transform-origin定義旋轉的基點可為left top center right bottom 或座標值(50px 70px)。 skew 扭曲 transform:skew(30deg,30deg) skew(相對x軸傾斜,相對y軸傾斜) scale 縮放 transform:scale(2,3) 橫向放大2倍,縱向放大3倍。如等比放大寫一個參數即可。 translate 移動 transform:translate(50px, 50px);matrix 矩陣變形 基本文法transform: matrix(a, c, b, d, tx, ty);其中a, c, b, d是一個二維矩陣:
┌ ┐ │ a b │ │ c d │ └ ┘
a:X軸縮放比例 b:Y軸傾斜 c:Y軸縮放比例 d:X軸傾斜
tx, ty就是就是基於X和Y 座標重新置放元素。其實就是translate (tx,ty)
Transition
W3C標準中對CSS3的transition這是樣描述的:“CSS的transition允許CSS的屬性值在一定的時間區間內平滑地過渡。這種效果可以在按一下滑鼠、獲得焦點、被點擊或對元素任何改變中觸發,並圓滑地以動畫效果改變CSS的屬性值。
transition主要包含四個屬性值:transition-property: 執行變換的屬性;
transition- duration: 變換延續的時間:
transition-timing-function: 在延續時間段,變換的速率變化;
transition- delay :變換延遲時間
下面一一說明這四個屬性:
transition-property
文法:
transition-property : none | all | [ <IDENT> ]
transition-property是用來指定當元素其中一個屬性改變時執行transition效果,其 主要有以下幾個值:none(沒有屬 性改 變);all(所有屬性改變)這個也是其預設值;indent(元素屬性名稱);當其值為none時,transition馬上停止執行,當指定為all 時,則元素產生任何屬性值變化時都將執行transition效果,ident是可以指定元素的某一個屬性值。其對應的類型如下:
1、color: 通過紅、綠、藍和透明度組件變換(每個數值單獨處理),如:background-color,border-color,color,outline-color等CSS屬性;
2、length:真實的數字,如:word-spacing,width,vertical- align,top,right,bottom,left,padding,outline-width,margin,min-width,min- height,max-width,max-height,line-height,height,border-width,border- spacing,background-position等屬性;
3、percentage:真實的數字,如:word-spacing,width,vertical- align,top,right,bottom,left,min-width,min- height,max-width,max-height,line-height,height,background-position等屬性;
4、integer 離散步驟(整個數字),在真實的數字空間,以及使用floor()轉換為整數時發生,如:outline-offset,z-index等屬性;
5、number真實的(浮點型)數值,如:zoom,opacity,font-weight等屬性;
6、transform list:詳情請參閱:《CSS3 Transform》。
7、rectangle:通過x、 y、 width和height(轉為數值)變換,如:crop;
8、visibility:離散步驟,在0到1數字範圍之內,0表示“隱藏”,1表示完全“顯示”,如:visibility;
9、shadow:作用於color、x、y、和blur(模糊)屬性,如:text-shadow;
10、gradient:通過每次停止時的位置和顏色進行變化。它們必須有相同的類型(放射狀的或是線性)和相同的停止數值以便執行動畫,如:background-image;
11、paint server (SVG):只支援下面的情況:從gradient到gradient以及color到color,然後工作與上面類似;
12、space-separated list of above:如果列表有相同的項目數值,則列表每一項按照上面的規則進行變化,否則無變化;
13、a shorthand property:如果縮寫的所有部分都可以實現動畫,則會像所有單個屬性變化一樣變化。
支援執行transition效果的屬性:
| Property Name |
Type |
| background-color |
as color |
| background-position |
as repeatable list of simple list of length, percentage, or calc |
| border-bottom-color |
as color |
| border-bottom-width |
as length |
| border-left-color |
as color |
| border-left-width |
as length |
| border-right-color |
as color |
| border-right-width |
as length |
| border-spacing |
as simple list of length |
| border-top-color |
as color |
| border-top-width |
as length |
| bottom |
as length, percentage, or calc |
| clip |
as rectangle |
| color |
as color |
| font-size |
as length |
| font-weight |
as font weight |
| height |
as length, percentage, or calc |
| left |
as length, percentage, or calc |
| letter-spacing |
as length |
| line-height |
as either number or length |
| margin-bottom |
as length |
| margin-left |
as length |
| margin-right |
as length |
| margin-top |
as length |
| max-height |
as length, percentage, or calc |
| max-width |
as length, percentage, or calc |
| min-height |
as length, percentage, or calc |
| min-width |
as length, percentage, or calc |
| opacity |
as number |
| outline-color |
as color |
| outline-width |
as length |
| padding-bottom |
as length |
| padding-left |
as length |
| padding-right |
as length |
| padding-top |
as length |
| right |
as length, percentage, or calc |
| text-indent |
as length, percentage, or calc |
| text-shadow |
as shadow list |
| top |
as length, percentage, or calc |
| vertical-align |
as length |
| visibility |
as visibility |
| width |
as length, percentage, or calc |
| word-spacing |
as length |
| z-index |
as integer |
transition-durationtransition-duration是用來指定元素 轉換過程的期間,取值:<time>為數值,單位為s(秒),可以作用於所有元素,包括:before和:after虛擬元素。其預設值是0,也就是變換時是即時的。 transition-timing-function
取值:
transition-timing-function的值允許你根據時間的推進去改變屬性值的變換速率,transition-timing-function有6個可能值:
1、ease:(逐漸層慢)預設值,ease函數等同於貝茲路徑(0.25, 0.1, 0.25, 1.0);
2、linear:(勻速),linear 函數等同於貝茲路徑(0.0, 0.0, 1.0, 1.0);
3、ease-in:(加速),ease-in 函數等同於貝茲路徑(0.42, 0, 1.0, 1.0);
4、ease-out:(減速),ease-out 函數等同於貝茲路徑(0, 0, 0.58, 1.0);
5、ease-in-out:(加速然後減速),ease-in-out 函數等同於貝茲路徑(0.42, 0, 0.58, 1.0);
6、cubic-bezier:(該值允許你去自訂一個時間曲線), 特定的cubic-bezier曲線。 (x1, y1, x2, y2)四個值特定於曲線上點P1和點P2。所有值需在[0, 1]地區內,否則無效。
其是cubic-bezier為通過貝賽爾曲線來計算“轉換”過程中的屬性值,如下曲線所示,通過改變P1(x1, y1)和P2(x2, y2)的座標可以改變整個過程的Output Percentage。初始預設值為default。
transition-delay
transition-delay是用來指定一個動畫開始執行的時間,也就是說當改變元素屬性值後多長時間開始執行transition效果,取 值:<time>為數值,單位為s(秒),它的使用和transition-duration極其相似,也可以作用於所有元素,包 括:before和:after虛擬元素。 預設大小是”0″,也就是變換立即執行,沒有延遲。
有時我們不只改變一個CSS效果的屬性,而是想改變兩個或者多個CSS屬性的transition效果,那麼我們只要把幾個transition的 聲明串 在一起,用逗號(“,”)隔開,然後各自可以有各自不同的延續時間和其時間的速率變換方式。但需要值得注意的一點:transition-delay與 transition-duration的值都是時間,所以要區分它們在連寫中的位置,一般瀏覽器會根據先後順序決定,第一個可以解析為時間的怭值為 transition-duration第二個為transition-delay。如:
a {transition: background 0.5s ease-in,color 0.3s ease-out}
如果你想給元素執行所有transition效果的屬性,那麼我們還可以利用all屬性值來操作,此時他們共用同樣的延續時間以及速率變換方式,如:
a{transition: all 0.5s ease-in}
}
animation
顧名思義為動畫的意思。Animation應用在頁面DOM上 使其產生動畫的效果。在開始介紹Animation之前我們有必要先來瞭解一個特殊的東西,那就是"Keyframes",我們把他叫做“主要畫面格”,玩過flash的朋友可能對這個東西並不會陌生。
一個官網的樣本:
@-webkit-keyframes ‘wobble‘ { 0% { margin-left: 100px; background: green; } 40% { margin-left: 150px; background: orange; } 60% { margin-left: 75px; background: blue; } 100% { margin-left: 100px; background: red; } }
這裡我們定義了一個叫“wobble”的動畫,名字任意取。分幾個階段0% 40% 60% 100% 來過渡。
keyframes定義好了以後,就可以去調用定義好的動畫“wobble”了。
下面我們來看看怎麼給一個元素調用animation屬性
.demo1 { width: 50px; height: 50px; margin-left: 100px; background: blue; -webkit-animation-name:‘wobble‘;/*動畫屬性名稱,也就是我們前面keyframes定義的動畫名*/ -webkit-animation-duration: 10s;/*動畫期間*/ -webkit-animation-timing-function: ease-in-out; /*動畫頻率,和transition-timing-function是一樣的*/ -webkit-animation-delay: 2s;/*動畫延遲時間*/ -webkit-animation-iteration-count: 10;/*定義迴圈資料,infinite為無限次*/ -webkit-animation-direction: alternate;/*定義動畫方式*/ }
animation-name:
animation-name:是用來定義一個動畫的名稱,為Keyframes中的名稱,否則不會有動畫效果。none為預設值,當值為none時,將沒有任何動畫效果。另外我們這個屬性跟前面所講的transition一樣,我們可以同時附幾個animation給一個元素,我們只需要用逗號“,”隔開。
CSS3的animation類似於transition屬性,他們都是隨著時間改變元素的屬性值。他們主要區別是transition需要觸發一 個事件(hover事件或click事件等)才會隨時間改變其css屬性;而animation在不需要觸發任何事件的情況下也可以顯式的隨著時間變化來 改變元素css的屬性值,從而達到一種動畫的效果。這樣我們就可以直接在一個元素中調用animation的動畫屬性,基於這一點,css3的 animation就需要明確的動畫屬性值,這也就是回到我們上面所說的,我們需要keyframes來定義不同時間的css屬性值,達到元素在不同時間 段變化的效果。
<script type="text/javascript"><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首頁橫幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
html從入門到買電腦(三)