天天看點

CSS動畫:Transition與Animation差異比較浏覽器支援

本文總結CSS3中兩個用來做動畫的屬性,一個是

transition

,另一個是

animation

差異比較

CSS3 差異

transition

在給定的持續時間内平滑地更改屬性值(從一個值到另一個值),也就是隻需要指定開始與結束的參數,參數改變時就觸發動畫。
常用語滑鼠事件(

:hover

active

:focus

:click

)或鍵盤輸入時觸發
需要事件觸發,無法在網頁加載時自動發生。一次性,不能重複發生,除非一再觸發。
隻能定義開始狀态和結束狀态,不能定義中間狀态。

animation

可以自行寫動畫開始、進行間、結束時各階段的變化,适合用來做較細微的動畫表現。需要明确的指定關鍵幀(

@keyframe

)的參數。
網頁加載時會直接執行,可以自行控制各階段動畫的變化

animation

transition

最大的不同在于

transition

是當參數改變時觸發,而

animation

則是直接就執行,所有動畫效果需要明确的指定關鍵幀的參數。
CSS3 簡寫順序

transition

property

名稱

timing-function

特效

animation

name

名稱

timing-function

特效

iteration-count

次數

fill-mode

填充模式

浏覽器支援

transition

寫法

.box {
  width: 100px;
  height: 100px;
  background-color: purple;
  transition: width 2s ease-in 2s;
}

.box:hover {
  width: 200px;
  height: 200px;
  background-color: red;
}
           

animation

寫法

.box {
  width: 100px;
  height: 100px;
  border: 1px solid #ccc;
  animation: change 5s; /*8個屬性中至少要有名稱、時間*/
}

/*設定開始與結束狀态*/
/*from 就是0%,to 就是100%*/
@keyframes change {
  from {
    background-color: #4BC0C8;
  }
  to {
    background-color: #C779D0;
  }
}
           
.box {
  width: 100px;
  height: 100px;
  border: 1px solid #ccc;
  animation: change 5s; /*8個屬性中至少要有名稱、時間*/
}

/*設定開始與結束狀态*/
/*from 就是0%,to 就是100%*/
@keyframes change {
  0% {
    background-color: #4BC0C8;
  }
  20% {
    background-color: #C779D0;
  }
  60% {
    background-color: #FEAC5E;
  }
  80% {
    background-color: #185a9d;
  }
  100% {
    background-color: #4BC0C8;
  }
}
web前端開發資源Q-q-u-n: 767273102 ,分享開發工具,零基礎,進階視訊教程,希望新手少走彎路
           
屬性

animation-name

@keyframes

後的名稱

animation-duration

時間

time

以秒計算,如

3s initial

預設值

inherit

繼承父層

animation-timing-function

特效

linear

等速、

ease

ease-in

ease-out

ease-in-out

step-start

step-end

steps(int,start/end)

cubic-bezier(n,n,n,n)

可上官網取值使用

animation-delay

以秒計算,如

2s

animation-iteration-count

次數

number

預設值為

1

,是以填

2

時,動畫跑的次數為

1+2=3

infinite

無限循環

animation-direction

方向

normal

reverse

反向、

alternate

先反後正

animation-fill-mode

forwards

使用關鍵幀最後的值

backwards

使用最開始的值

both

animation-play-state

播放狀态

pause

暫停

running

為預設值

initial

預設值、

inherit

繼承父層

Animation.css

官網下載下傳:Animate.css