CSS3 提供了transition 過渡
、transform 變換
和animation 動畫
來實現頁面中的一些樣式轉化,這篇文章會對這幾個屬性作簡單的介紹,而後比較一下 CSS3 動畫和 JS 動畫哪一個性能更好。css
transition容許css的屬性值在必定的時間區間內平滑地過渡,語法以下:性能
transition : transition-property transition-duration transition-timing-function transition-delay [, ...]
none
,all
或者特定的屬性。s(秒)
或者 ms(毫秒)
。ease|linear|ease-in|ease-out|ease-in-out|cubic-bezier(自定義時間曲線)
。transition-duration
,可是能夠爲負數。DEMO:http://codepen.io/CodingMonkeyzh/pen/ZGBRVe動畫
transform 分爲2D 和 3D,這裏暫時只介紹比較經常使用的2D transform,其主要包含如下幾種變換:旋轉rotate、扭曲skew、縮放scale和移動translate以及矩陣變形matrix,語法以下:code
transform: rotate | scale | skew | translate |matrix;
deg 度
,正數表示順時針旋轉,負數表示逆時針旋轉。0~n
,小於1
時表示縮小,反之表示放大。例如scale(0.5, 2)
表示水平方向縮小1倍,垂直方向放大1倍, 另外,也能夠經過scaleX
或者scaleY
對一個方向進行設置。rotate
同樣都是deg 度
。例如 skew(30deg, 10deg)
表示水平方向傾斜30度,垂直方向傾斜10度。translate(x,y)
水平方向和垂直方向同時移動(也就是X軸和Y軸同時移動);translateX(x)
僅水平方向移動(X軸移動);translateY(Y)
僅垂直方向移動(Y軸移動)。CSS3 中的 animation 是經過一個叫Keyframes 關鍵幀
的玩意來控制的,他的命名是由"@keyframes"開頭,後面緊接着是這個「動畫的名稱」加上一對花括號「{}」,括號中就是一些不一樣時間段樣式規則,有點像咱們css的樣式寫法同樣。對於一個"@keyframes"中的樣式規則是由多個百分比構成的,如「0%」到"100%"之間,語法以下:orm
@keyframes IDENT { from { Properties: Properties value; } Percentage { Properties: Properties value; } to { Properties: Properties value; } } 或者所有寫成百分比的形式: @keyframes IDENT { 0% { Properties: Properties value; } Percentage { Properties: Properties value; } 100% { Properties: Properties value; } }
animation和transition同樣有本身相對應的屬性,那麼在animation主要有如下幾種:animation-name;animation-duration;animation-timing-function;animation-delay;animation-iteration-count;animation-direction;animation-play-state。下面對其中的一些屬性進行解釋:get
keyframes
建立的動畫名,默認值爲none
,當值爲none
時,將沒有任何動畫效果。若是咱們要同時附幾個animation
給一個元素,只要用逗號,
隔開便可。1
,若是要進行無限循環,只要設爲infinite
便可。normal
,若是設置爲normal時,動畫的每次循環都是向前播放;另外一個值是alternate
,他的做用是,動畫播放在第偶數次向前播放,第奇數次向反方向播放。DEMO 1: http://codepen.io/CodingMonkeyzh/pen/mJOKZYanimation