2015-11-17 60 views
4

我正在處理SVG線條繪製動畫,如下所示。如何控制CSS3動畫的時間

enter image description here

所以我在做什麼是使用stroke-dasharray動畫耳朵,然後繼續點。

我設法完成了耳朵的繪製,但我不知道如何控制點的時間。以下是我的動畫步驟:5s內

    1. 耳繪圖動畫後5秒,繼續點動畫(按行動畫行)
    2. 當完成點動畫,循環回第一步

    這是我的代碼,但它逐漸閃爍。我如何調整時間?

    .firstpath { 
     
        stroke-dasharray: 1500; 
     
        animation: firstanimate 5s linear forwards infinite; 
     
    } 
     
    @keyframes firstanimate { 
     
        from { 
     
        stroke-dashoffset: 1500; 
     
        } 
     
        to { 
     
        stroke-dashoffset: 0; 
     
        } 
     
    } 
     
    .secondpath { 
     
        stroke-dasharray: 500; 
     
        animation: secondanimate 5s linear forwards infinite; 
     
    } 
     
    @keyframes secondanimate { 
     
        from { 
     
        stroke-dashoffset: 500; 
     
        } 
     
        to { 
     
        stroke-dashoffset: 0; 
     
        } 
     
    } 
     
    .thirdpath { 
     
        stroke-dasharray: 500; 
     
        animation: thirdanimate 5s linear forwards infinite; 
     
    } 
     
    @keyframes thirdanimate { 
     
        from { 
     
        stroke-dashoffset: 500; 
     
        } 
     
        to { 
     
        stroke-dashoffset: 0; 
     
        } 
     
    } 
     
    .row1col1 { 
     
        animation-delay: 1s; 
     
        animation: blink 2s step-start 0s infinite; 
     
        -webkit-animation: blink 2s step-start 0s infinite; 
     
    } 
     
    @keyframes blink { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        50% { 
     
        opacity: 0.0; 
     
        } 
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    } 
     
    @-webkit-keyframes blink { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        50% { 
     
        opacity: 0.0; 
     
        } 
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    }
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="841.89px" height="595.28px" viewBox="0 0 841.89 595.28" enable-background="new 0 0 841.89 595.28" xml:space="preserve"> 
     
    
     
        <g> 
     
        <path class="firstpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M253.441,284.167c0,0-0.883-14.535,15.429-18.551c5.364-1.32,15.943-0.665,21.667,6.79c13.859,18.051-3.235,32.286-8.087,37.262 
     
    \t \t \t c-4.853,4.979-7.713,21.027-17.543,20.528c-9.829-0.497-12.69-6.717-12.317-11.818" /> 
     
    
     
        <path class="secondpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M285.249,285.252c0,0,0.654-8.212-8.864-10.544c-9.519-2.334-16.796,8.211-10.825,17.449c2.613-1.12,5.226,0.652,5.599,2.52 
     
    \t \t \t c0.373,1.866-1.213,4.199-3.826,4.199" /> 
     
    
     
        <path class="thirdpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M257.815,307.553c0,0-3.732,5.879,2.333,8.118c6.065,2.241,5.318-8.118,8.865-9.237c3.545-1.119,11.943-2.519,11.943-10.265 
     
    \t \t \t c0-7.744-6.398-16.451-17.522-11.958" /> 
     
        </g> 
     
    
     
        <line class="row1col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="287.987" x2="324.156" y2="286.627" /> 
     
    
     
        <line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="285.335" x2="332.744" y2="284.167" /> 
     
    
     
        <line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="338.25" y1="301.171" x2="343.41" y2="301.171" /> 
     
    
     
        <line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="301.171" x2="333.75" y2="301.171" /> 
     
    
     
        <line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="301.171" x2="324.156" y2="301.171" /> 
     
    
     
        <line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="313.741" y1="314.625" x2="319.41" y2="315.987" /> 
     
    
     
        <line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="323.741" y1="316.746" x2="329.667" y2="318.142" /> 
     
    
     
    </svg>

  • +0

    我希望我的回答更新的摘要內容完全解決你的問題。如果確實如此,請考慮接受答案(點擊答案旁邊的投票按鈕下方的空白刻度標記)。這通常是我們在這裏將問題標記爲*解決*的方式。 – Harry

    +0

    是的,你做到了。但我需要調整時間以使其更快。非常感謝你的回答。昨天投票。 – Eelyn

    +1

    對不起,誤解,我想你說的是投票。我點擊了它,因爲你的答案解決了我的問題。 :) – Eelyn

    回答

    3

    形成形狀的零件的方式,這需要多個關鍵幀動畫工作,你爲它準備的方式。

    下面是你必須做的:

    • 耳動畫雖然它預計將在5s馬克完成,直到線的動畫完成不應該啓動它的下一次迭代。也就是說,在線路完成動畫所花費的時間內,耳朵應保持閒置狀態。因此,我們必須爲耳朵設置animation-duration,以便涵蓋線路完成其動畫所需的時間量。在這裏,每一行組件佔用1s,因此所有元素(耳和線)的合計animation-duration將爲12s
    • 耳朵動畫應在5s標記處完成(總共持續時間爲12s),因此from - to關鍵幀設置應該用百分比代替。人耳動畫應從0%開始,並在41%(大致爲5s標誌)完成。從那裏開始,它應該保持其位置,直到100%12s)標記(基本上直到線條完成其動畫)。
    • 每一行組件應在其前一部分完成其動畫之後開始,因此row1col1應該從41%開始,即當耳朵變得完全可見時(直到它應該保持不可見)。
    • row1col1需要1s動畫(這大概是8%12s的),因此所述row1col2應該等待,直到總的持續時間的49%開始其動畫。同樣row2col1應該從開始,row2col2應該從66%開始等等。
    • 如果線條需要慢慢淡出,然後使它在41%opacity: 0啓動和49%得到opacity: 1(用於row1col1等的其餘部分)。另一方面,如果您需要它們瞬間出現,請減少它獲得的百分比opacity: 1。在片段中,我已經使row1col1本身達到了opacity: 1,42%(這就像0.12s)。

    注:

    • 我還修改了耳朵的成分的stroke-dasharray設置,以避免原本有耳朵的動畫的完成和生產線的動畫開始之間的延遲。
    • 我也建議你得到一些SVG專家的幫助來檢查是否。的零件可以最小化,因爲它會減少no。需要的關鍵幀等。不幸的是,我的SVG知識是有限的,所以我無法幫助它。

    .firstpath { 
     
        stroke-dasharray: 150; 
     
        animation: firstanimate 12s linear forwards infinite; 
     
    } 
     
    @keyframes firstanimate { 
     
        0% { 
     
        stroke-dashoffset: 150; 
     
        } 
     
        41% { 
     
        stroke-dashoffset: 0; 
     
        } 
     
    } 
     
    .secondpath { 
     
        stroke-dasharray: 100; 
     
        animation: secondanimate 12s linear forwards infinite; 
     
    } 
     
    @keyframes secondanimate { 
     
        0% { 
     
        stroke-dashoffset: 100; 
     
        } 
     
        41% { 
     
        stroke-dashoffset: 0; 
     
        } 
     
    } 
     
    .thirdpath { 
     
        stroke-dasharray: 65; 
     
        animation: thirdanimate 12s linear forwards infinite; 
     
    } 
     
    @keyframes thirdanimate { 
     
        0% { 
     
        stroke-dashoffset: 65; 
     
        } 
     
        41% { 
     
        stroke-dashoffset: 0; 
     
        } 
     
    } 
     
    line { 
     
        opacity: 0; 
     
    } 
     
    .row1col1 { 
     
        animation: blink 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink { 
     
        41% { 
     
        opacity: 0; 
     
        } 
     
        42%, 100% { 
     
        opacity: 1; 
     
        } 
     
    
     
    } 
     
    .row1col2 { 
     
        animation: blink2 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink2 { 
     
        49% { 
     
        opacity: 0; 
     
        } 
     
        50%, 100% { 
     
        opacity: 1; 
     
        } 
     
    } 
     
    .row2col1 { 
     
        animation: blink3 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink3 { 
     
        57% { 
     
        opacity: 0; 
     
        } 
     
        58%, 100% { 
     
        opacity: 1; 
     
        } 
     
    } 
     
    .row2col2 { 
     
        animation: blink4 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink4 { 
     
        66% { 
     
        opacity: 0; 
     
        } 
     
        67%, 100% { 
     
        opacity: 1; 
     
        } 
     
    } 
     
    .row2col3 { 
     
        animation: blink5 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink5 { 
     
        75% { 
     
        opacity: 0; 
     
        } 
     
        76%, 100% { 
     
        opacity: 1; 
     
        } 
     
    } 
     
    .row3col1 { 
     
        animation: blink6 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink6 { 
     
        84% { 
     
        opacity: 0; 
     
        } 
     
        85%, 100% { 
     
        opacity: 1; 
     
        } 
     
    } 
     
    
     
    .row3col2 { 
     
        animation: blink7 12s linear forwards infinite; 
     
    } 
     
    @keyframes blink7 { 
     
        92% { 
     
        opacity: 0; 
     
        } 
     
        93%, 100% { 
     
        opacity: 1; 
     
        } 
     
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script> 
     
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="841.89px" height="595.28px" viewBox="0 0 841.89 595.28" enable-background="new 0 0 841.89 595.28" xml:space="preserve"> 
     
    
     
        <g> 
     
        <path class="firstpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M253.441,284.167c0,0-0.883-14.535,15.429-18.551c5.364-1.32,15.943-0.665,21.667,6.79c13.859,18.051-3.235,32.286-8.087,37.262 
     
    \t \t \t c-4.853,4.979-7.713,21.027-17.543,20.528c-9.829-0.497-12.69-6.717-12.317-11.818" /> 
     
    
     
        <path class="secondpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M285.249,285.252c0,0,0.654-8.212-8.864-10.544c-9.519-2.334-16.796,8.211-10.825,17.449c2.613-1.12,5.226,0.652,5.599,2.52 
     
    \t \t \t c0.373,1.866-1.213,4.199-3.826,4.199" /> 
     
    
     
        <path class="thirdpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M257.815,307.553c0,0-3.732,5.879,2.333,8.118c6.065,2.241,5.318-8.118,8.865-9.237c3.545-1.119,11.943-2.519,11.943-10.265 
     
    \t \t \t c0-7.744-6.398-16.451-17.522-11.958" /> 
     
        </g> 
     
    
     
        <line class="row1col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="287.987" x2="324.156" y2="286.627" /> 
     
    
     
        <line class="row1col2" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="285.335" x2="332.744" y2="284.167" /> 
     
    
     
        <line class="row2col3" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="338.25" y1="301.171" x2="343.41" y2="301.171" /> 
     
    
     
        <line class="row2col2" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="301.171" x2="333.75" y2="301.171" /> 
     
    
     
        <line class="row2col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="301.171" x2="324.156" y2="301.171" /> 
     
    
     
        <line class="row3col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="313.741" y1="314.625" x2="319.41" y2="315.987" /> 
     
    
     
        <line class="row3col2" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="323.741" y1="316.746" x2="329.667" y2="318.142" /> 
     
    
     
    </svg>

    +0

    哇,很酷!這就是我要的。非常感謝!如何消除淡入淡出效果? – Eelyn

    +0

    @Eelyn:對不起,沒有通過淡入淡出效果得到你所指的。你想讓這些線條瞬間出現而不是淡入? – Harry

    +0

    是的,這就是我的意思。對不起,因爲我沒有清楚解釋。 – Eelyn

    1

    您可以用動畫時間玩。延遲僅適用於第一個動畫。 您還應該將未添加前綴的屬性移到前面。

    我做了一個動畫,每5秒鐘閃爍2秒(共5秒)。

    .firstpath { 
     
        stroke-dasharray: 1500; 
     
        animation: firstanimate 5s linear forwards infinite; 
     
    } 
     
    
     
    @keyframes firstanimate { 
     
        from { stroke-dashoffset: 1500; } 
     
        to { stroke-dashoffset: 0; } 
     
    } 
     
    
     
    .secondpath { 
     
        stroke-dasharray: 500; 
     
        animation: secondanimate 5s linear forwards infinite; 
     
    } 
     
    
     
    @keyframes secondanimate { 
     
        from { stroke-dashoffset: 500; } 
     
        to { stroke-dashoffset: 0; } 
     
    } 
     
    
     
    .thirdpath { 
     
        stroke-dasharray: 500; 
     
        animation: thirdanimate 5s linear forwards infinite; 
     
    } 
     
    
     
    @keyframes thirdanimate { 
     
        from { stroke-dashoffset: 500; } 
     
        to { stroke-dashoffset: 0; } 
     
    } 
     
    
     
    
     
    .row1col1 { 
     
        animation: blink1 5s step-start 0s infinite; 
     
        -webkit-animation: blink1 5s step-start 0s infinite; 
     
    } 
     
    @-webkit-keyframes blink1 { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        70% { 
     
        opacity: 1.0; 
     
        } 
     
        
     
        80% { 
     
        opacity: 0.0; 
     
        } 
     
        
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    } 
     
    
     
    @keyframes blink1 { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        70% { 
     
        opacity: 1.0; 
     
        } 
     
        80% { 
     
        opacity: 0.0; 
     
        } 
     
        
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    } 
     
    
     
    .row1col2 { 
     
        animation: blinkl2 5s step-start 0s infinite; 
     
        -webkit-animation: blink2 5s step-start 0s infinite; 
     
    } 
     
    @-webkit-keyframes blink2 { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        80% { 
     
        opacity: 1.0; 
     
        } 
     
        
     
        90% { 
     
        opacity: 0.0; 
     
        } 
     
        
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    } 
     
    @keyframes blink2 { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        80% { 
     
        opacity: 1.0; 
     
        } 
     
        
     
        90% { 
     
        opacity: 0.0; 
     
        } 
     
        
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    } 
     
    .row1col3 { 
     
        animation: blinkl3 5s step-start 0s infinite; 
     
        -webkit-animation: blink3 5s step-start 0s infinite; 
     
    } 
     
    @-webkit-keyframes blink2 { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        90% { 
     
        opacity: 1.0; 
     
        } 
     
        
     
        95% { 
     
        opacity: 0.0; 
     
        } 
     
        
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    } 
     
    @keyframes blink3 { 
     
        0% { 
     
        opacity: 1.0; 
     
        } 
     
        90% { 
     
        opacity: 1.0; 
     
        } 
     
        
     
        95% { 
     
        opacity: 0.0; 
     
        } 
     
        
     
        100% { 
     
        opacity: 1.0; 
     
        } 
     
    }
    <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" 
     
    \t width="841.89px" height="595.28px" viewBox="0 0 841.89 595.28" enable-background="new 0 0 841.89 595.28" xml:space="preserve"> 
     
    \t 
     
    \t <g> 
     
    \t \t <path class="firstpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M253.441,284.167c0,0-0.883-14.535,15.429-18.551c5.364-1.32,15.943-0.665,21.667,6.79c13.859,18.051-3.235,32.286-8.087,37.262 
     
    \t \t \t c-4.853,4.979-7.713,21.027-17.543,20.528c-9.829-0.497-12.69-6.717-12.317-11.818"/> 
     
    \t \t \t 
     
    \t \t <path class="secondpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M285.249,285.252c0,0,0.654-8.212-8.864-10.544c-9.519-2.334-16.796,8.211-10.825,17.449c2.613-1.12,5.226,0.652,5.599,2.52 
     
    \t \t \t c0.373,1.866-1.213,4.199-3.826,4.199"/> 
     
    \t \t \t 
     
    \t \t <path class="thirdpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d=" 
     
    \t \t \t M257.815,307.553c0,0-3.732,5.879,2.333,8.118c6.065,2.241,5.318-8.118,8.865-9.237c3.545-1.119,11.943-2.519,11.943-10.265 
     
    \t \t \t c0-7.744-6.398-16.451-17.522-11.958"/> 
     
    \t </g> 
     
    
     
    \t <line class="row1col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="287.987" x2="324.156" y2="286.627"/> 
     
    
     
    \t <line class="row1col2" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="285.335" x2="332.744" y2="284.167"/> 
     
    
     
    \t <line class="row1col3" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="338.25" y1="301.171" x2="343.41" y2="301.171"/> 
     
    
     
    \t <line class="row1col2" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="301.171" x2="333.75" y2="301.171"/> 
     
    
     
    \t <line class="row1col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="301.171" x2="324.156" y2="301.171"/> 
     
    
     
    \t <line class="row1col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="313.741" y1="314.625" x2="319.41" y2="315.987"/> 
     
    
     
    \t <line class="row1col2" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="323.741" y1="316.746" x2="329.667" y2="318.142"/>

    +0

    酷!非常感謝。我會自己調整動畫。 – Eelyn