CSS動畫-transition/animation

image.png

HTML系列:

CSS系列:

00、CSS動畫

CSS系列:

CSS 動畫就是由一種狀態(CSS樣式),過渡到另一中狀態(CSS樣式)的動態過程,CSS中提供了兩種動畫實現方式:transitionanimation

對比 transition動畫 animation動畫
定義 基於CSS屬性變化的簡單過渡動畫 基於關鍵幀@keyframes實現更複雜的動畫
復用 只能執行一次,不可重複執行,復用不便 可多次執行,復用方便
執行方式 頁面加載不會默認執行,須觸發執行 可直接執行、可控制
動畫事件 無,只能預估動畫時間,用定時器setTimeout模擬 支持監聽事件,如動畫開始、結束
動畫幀 只有初始幀(當前樣式)、結束幀(觸發動畫時的樣式) 支持任意多幀動畫設置
五星好評 靈活簡單 功能豐富

01、transition過渡動畫

1.1、transition動畫

transition 過渡動畫是針對CSS樣式的變化,進行過渡,如widthopacitycolor改變的過渡,可以實現CSS樣式的平滑動態過渡動畫效果。

transition 動畫本身並不會主動執行,須通過其他方式觸發,動畫屬性變化才會觸發執行。常用一些偽類觸發::hover:focus:active(鼠標按下激活)、:target(錨點元素id)、:checked,或者JS控制CSS樣式來觸發動畫執行。

transition過渡 描述 示例
transition 過渡動畫的簡寫屬性,包括下面這些小弟 (transition /trænˈzɪʃ(ə)n/ 過渡)
transition-property 指定過渡動畫的CSS屬性名,多個,分割,默認all都生效 transition-property: width;
transition-duration 動畫時長,默認0,單位s、ms,*必備 transition-duration: 1s;
transition-delay 動畫延時時長,延時執行動畫 transition-delay: 1s;
*-timing-function 指定過渡動畫執行緩動曲線函數,詳見後面animation章節 transition-timing-function: linear;

🔸簡寫屬性:transiton: property duration timing-function delay

transition-property指定的屬性值變化時,就會觸發動畫執行,且只對該屬性執行過渡動畫,設置all則任意屬性變化都會觸發動畫執行。

1.gif

如下示例分析:

  • 頁面初始加載時並不會觸發動畫執行。
  • 當鼠標移入時,屬性widthbackground-color值變化,觸發了動畫執行。
  • 當鼠移出時,屬性widthbackground-color值回到初始狀態,再次觸發了動畫執行。
<div>
    <button onclick="active()">動起來</button>	
    <p class="goodstudy">好好學習</p>
</div>
<style>
    .goodstudy {
        background-color: #63a9e7;
        width: 150px; margin: 40px 0; padding: 8px;
        /* 設置動畫 頁面加載並不會執行 */
        transition-property: width,background-color;
        transition-duration: 1s;
        transition-delay: 0.2s;
        transition-timing-function: ease-out;
        transition: all 1s ease-out; 
    }
    .goodstudy:hover {
        width: 350px;
        background-color: red;
    }
    .active {
        transform: rotate(360deg);
        background-color: #0cdb39;
        transition: all 3s;
    }
</style>
<script>
    //通過腳本添加CSS類,觸發動畫執行
    const pnode = document.querySelector('.goodstudy');
    function active() {
        pnode.classList.add('active');
        //執行完移除,沒有事件只能定時執行移除動作
        setTimeout(() => {
            console.log('removed');
            pnode.classList.remove('active');
        }, 3000);
    }
</script>

1.2、transform變換

transform 可實現元素的各種圖形變換,如縮放、旋轉,及3D的變換,(transform /trænsˈfɔːrm/ 變換)。transform 本身並不是動畫,不過常用來配合transition來實現各種炫酷的變換動畫效果。

transform變換函數 描述 示例
transform 元素變換,值支持下面這些函數,可設置多個值 transform: skew(30deg) rotate(60deg);
translate(x, y) 位移變換,x、y方向的移動,可負數。擴展函數translateX()、translateY(),其他變換函數類似 transform: translateY(100);
( translate /trænzˈleɪt/ 變化、移動)
scale(x, y) 縮放變換,1為100%原始大小 transform: scaleX(2);
rotate(angle) 旋轉,參數單位為角度deg,(rotate /rəʊˈteɪt/ ) transform: rotate(30deg);
skew(x, y) 元素傾斜,單位為角度deg( skew /skjuː/ 傾斜) transform: skew(-60deg,0);
translate3d(x,y,z) 3D的位置變換,指定x、y、z坐標軸的偏移距離 transform: translate3d(100px,0,0);
scale3d(x,y,z) 3D的縮放變換,指定x、y、z坐標軸的縮放值 transform: scale3d(2,1.2,1);
rotate3d(x,y,z,angle) 3D旋轉,指定x、y、z坐標軸 transform: rotateX(180deg);
matrix() 基於X軸和Y軸矩陣變換(/ˈmeɪtrɪks/矩陣)
其他變換相關屬性
transform-origin 元素中心坐標,默認center transform-origin: 150px 50px;
perspective 3D變換的透視視角,在父元素上設置 /pərˈspektɪv/ perspective: 500px;

3D坐標系的手勢圖:

image.png

<div>
    <button onclick="active()">動起來</button>
    <p class="goodstudy">好好學習</p>
    <p class="ddup">天天向上</p>
</div>
<style>
    .ddup{
        background-color: #0cdb39;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;

        transition: all 1s ease-out;
        transform: skew(-30deg);   
    }
    .ddup:hover{
        transform: translateX(-30px);   /* transform只有一個生效,被後面的覆蓋了*/
        transform: rotateX(180deg);     /* 圍繞x軸3d旋轉*/
    }
</style>

1.gif


02、animation幀動畫

CSS 動畫 animation幀動畫,動畫的實際CSS樣式是由 @keyframes 規則實現的,animation屬性負責設置動畫的各項運動參數。

2.1、animation

animation 屬性/值 描述 示例/備註
animation 動畫組合簡寫屬性,包括下面這些小弟 是有順序的,支持多組動畫,逗號隔開
animation-name *必填,指定由@keyframes定義的動畫序列名稱 @keyframes animation-name {}
animation-duration *必填,動畫時長,單位s、ms animation-duration: 2.5s
animation-iteration-count 動畫循環次數(1),infinite無限循環(/ˈɪnfɪnət/無限) animation-iteration-count: 3;
animation-timing-function 設置動畫速度變化函數,提供了函數、預置函數關鍵字 animation-timing-function: linear;
 linear、ease、… 預置的函數關鍵字定義,默認ease
cubic-bezier() 三次貝塞爾曲線函數,參數為兩個坐標點,在線工具 cubic-bezier(x1, y1, x2, y2)
animation-fill-mode 動畫執行元素樣式應用方式,默認none,動畫執行完成後恢復到原來的樣式。
animation-fill-mode: forwards;
forwards:動畫後保留最後一幀的樣式
backwards:立刻應用第一幀樣式,包括animation-delay延遲時間生效
both:forwards+backwards,全都要!
animation-delay 動畫延時時長,默認0s立即執行,可為負數 animation-delay: 2s;
animation-direction 播放方向方式,默認normalanimation-iteration-count多次執行時可以使用交替運行alternate alternate:動畫交替反向運行,結合多次
reverse:反向播放動畫
alternate-reverse:反向交替運行
animation-play-state 動畫運行狀態,running、paused,可用來控制動畫 animation-play-state: paused;

🔸簡寫屬性:animation: name duration timing-function delay iteration-count direction fill-mode play-state

<div class="div-abox">
    斷腸人在天涯
</div>
<style>
    .div-abox {
        padding: 4px;
        width: 150px;
        background-color: red;
        animation-delay: 1s;
        animation-duration: 1s;
        animation-name: box-line-ani;
        animation-direction: alternate;            /*動畫交替反向運行*/
        animation-iteration-count: infinite;       /*無限重複*/
        animation-fill-mode: both;
        animation-timing-function: cubic-bezier(.4, .52, .93, .4);
        /*animation 簡寫屬性*/
        animation: box-line-ani 1.5s alternate 4 cubic-bezier(.4, .52, .93, .4) both;
    }
    .div-abox:hover {   /* 鼠標懸浮時運動加速 */
        animation-duration: 0.5s;
    }
    @keyframes box-line-ani {
        0% {
            background-color: white;
            width: 150px;
        }
        40% { width: 250px; }
        100% {
            background-color: #63a9e7;
            width: 400px;
        }
    }
</style>

1.gif

2.2、@keyframes關鍵幀

animation 屬性定義動畫各項運動參數,實際的動畫執行的CSS屬性通過@keyframes來定義,使用@keyframes建立兩個或兩個以上關鍵幀來實現動畫幀的樣式定義。

@keyframes animationname { keyframes-selector { css-styles; } }

  1. 定一個關鍵幀動畫組,並命名:@keyframes animation-name {}
  2. 用百分比%來定動畫幀:
    • 0% 表示開始第一幀樣式,可以用別名from代替。
    • 100% 表示最後一幀樣式,可以用別名to代替。
    • 中間可以加其他%* 關鍵幀。
  3. 每一幀里定義需要執行動畫變換的CSS樣式。
@keyframes animation-name {
    0% {
        background-color: white;
        width: 150px;
    }
    40% { width: 250px; }
    100% {
        background-color: #63a9e7;
        width: 400px;
    }
}

2.3、animation-timing-function動畫緩動曲線

animation-timing-function 用來定義動畫執行過程的緩動速度,內置了幾個常用的函數定義關鍵字,及兩個關鍵函數。同transition 動畫中的緩動速度屬性 transition-timing-function 是一樣的,同母異父的親兄妹。

  • 三次貝塞爾曲線緩動函數cubic-bezier(x1, y1, x2, y2)(cubic /ˈkjuːbɪk/ 立方),參數其實是兩個坐標點,可以通過在線貝塞爾可視化工具編輯和測試。用來實現動畫過程中速度變化曲線的控制,以實現更自然的動畫效果。內置的linearease等都是基於貝塞爾曲線函數的。
  • 步驟緩動函數steps(),把@keyframes定義的動畫幀劃分為多段執行,多用來實現圖片的逐幀動畫效果。
animation-timing-function 描述 示例/補充
cubic-bezier(x1, y1, x2, y2) 三次貝塞爾曲線函數,參數為兩個坐標點,在線工具 cubic-bezier(x1, y1, x2, y2)
 linear 勻速,= cubic-bezier(0.0, 0.0, 1.0, 1.0) animation-timing-function: linear;
 ease 默認值:低速開始,中間加速,然後低速收尾 (ease /iːz/ 容易,減輕)
 ease-in 低速開始
 ease-out 低速結束
 ease-in-out 低速開始,低速結束
steps( n, <jumpterm>) 分階段緩動函數,參數為步數和變化點。可實現時鐘指針動畫 animation-timing-function:steps(6);

2.4、動畫事件

用於監聽動畫的開始、循環、結束的動畫事件 AnimationEvent

事件 描述
animationstart 動畫開始
animationend 動畫完成
animationiteration 動畫循環
<script>
    const node = document.querySelector('.div-abox');
    node.addEventListener('animationend', (e) => {
        console.log(e.animationName, e.type, e.elapsedTime);
        //box-line-ani animationend 1
    })
</script>

參考資料


©️版權申明:版權所有@安木夕,本文內容僅供學習,歡迎指正、交流,轉載請註明出處!原文編輯地址-語雀

Tags: