「CSS3 」動畫詳解

CSS3 提供給了咱們很是多的轉換函數:css

  • 2D: translate, rotate, scale, skew.
  • 3D: translate3d, rotate3d, scale3d, skew3d.

只須要將這些函數做爲transform屬性的值,就能夠設置相應的效果。html

CSS3 動畫則是將這些效果以及其餘CSS屬性按照你設定的方式來進行互相轉變。git

1. 動畫相關屬性

屬性 | 描述
--------------------------- | ------------------------------------------------------------------------------
@keyframes | 定義動畫各個階段的狀態的代碼段
animation | 全部動畫屬性的簡寫屬性,除了 animation-play-stateanimation-fill-mode 屬性。
animation-name | 規定 @keyframes 動畫的名稱。
animation-duration | 規定動畫完成一個週期所花費的秒或毫秒。默認是 0。
animation-timing-function | 規定動畫的速度曲線。默認是 ease
animation-delay | 規定動畫什麼時候開始。默認是 0。
animation-iteration-count | 規定動畫被播放的次數。默認是 1。
animation-direction | 規定動畫是否在下一週期逆向地播放。默認是 normal
animation-play-state | 規定動畫是否正在運行或暫停。默認是 running
animation-fill-mode | 規定元素在動畫開始前和完成後的狀態,默認是 nonegithub

@keyframes

定義動畫各個階段的狀態的代碼段。好比開始態,結束態,中間態(使用百分比表示)。bash

@keyframes exampleName {
    from {
        transform: rotateY(0deg);
        background: #000000;
    }
    50% {
        transform: rotateY(180deg);
        background: #00fa7e;
    }
    to {
        transform: rotateY(0deg);
        background: #008dff;
    }
}

animation-name

使用 @keyframes 定義的狀態集合名稱,如上面的 exampleName函數

animation-duration

動畫的週期時間。單位能夠是秒(s),也能夠是毫秒(ms)。測試

animation-timing-function

動畫變化速度函數,有以下幾種選項:字體

  • linear: 速度不變。cubic-bezier(0,0,1,1)
  • ease-in: 低速開始 ~ 正常速度。cubic-bezier(0.42,0,1,1)
  • ease-out: 正常速度 ~ 低速結束。cubic-bezier(0,0,0.58,1)
  • ease-in-out: 低速開始 ~ 正常速度 ~ 低速結束。cubic-bezier(0.42,0,0.58,1)
  • ease: 與 ease-in-out 基本相同,可是開始稍微比結束快一點兒。cubic-bezier(0.25,0.1,0.25,1)
  • cubic-bezier(x1, y1, x2, y2): 使用三次貝塞爾函數做爲速度函數。

cubic-bezier曲線測試調試網站:cubic-bezier動畫

animation-delay

動畫開始以前等待的時間。單位能夠是秒(s),也能夠是毫秒(ms)。網站

animation-iteration-count

動畫的循環次數。能夠是具體的次數,也能夠是 infinite,表示無限循環播放。

animation-direction

動畫循環的方向:

  • normal: 正向播放。
  • reverse: 反向播放。
  • alternate: 正向播放與反向播放交替進行。

animation

以上6個屬性能夠使用這一個屬性來表示,格式爲:

animate: name duration timing-function delay iteration-count direction;

animation-play-state

控制動畫的狀態,有播放(running)和暫停(paused)兩種狀態。

animation-fill-mode

規定元素在動畫開始前和完成後的狀態。

  • none: 元素在動畫先後的狀態和動畫沒有聯繫。
  • forwards: 動畫完成後,元素保持動畫最後的狀態。
  • backwards: 動畫開始前,元素保持動畫開始的狀態。
  • both: forwardsbackwards 的結合。

2. transition

CSS3 除了 animation 相關的屬性之外,還提供給咱們一個 transition 屬性,格式爲:

transition: propertyName duration [timing-function] [delay], ...;

你們可能已經也看出來了,其實 transition 就是 @keyframes 只有 fromto 兩個狀態,而且播放次數爲1,且不能暫停的 animation

舉個例子,當鼠標放到一個 div 上的時候,它旋轉90度,而且背景從黑色變爲灰色,字體從白色變爲黑色。

<div id="division2"></div>
#division2 {
    width: 300px;
    height: 100px;
    margin-top: 50px;
    font-size: 2em;
    text-align: center;
    line-height: 100px;
    color: #FFF;
    background: #000;
    transition: transform 2s linear 0s, background 2s linear 0s, color 2s linear 0s;
}

#division2:hover {
    background: #cccdd1;
    color: #000;
    transform: rotate(90deg);
}

3. Demo

[重要提示]請不要忘記推薦收藏 (╯‵□′)╯︵ ┴─┴

git clone https://github.com/JasonKid/fezone.git

搜索 動畫詳解

相關文章
相關標籤/搜索