2017-09-25 73 views
0

我正在使用Javascript處理幾個CSS類。我的腳本是按照時間順序排列的,它們一起播放動畫。我覺得我很接近,但我堅持,有什麼見解?基本上,我需要步驟1和步驟2在同一時間一起走完一秒的持續時間,步驟3在步驟1和2之後持續一秒,步驟4和步驟5一起走到一起在步驟1,2,3和4 ......之後的同一時間持續一秒鐘。在步驟6結束時,重置計時器並重復。我有一個SVG,它由一堆線組成。其中一些線條必須在序列中的某個時間「開啓」,有些必須變得更大,所以我只能在某段時間內切換課程,否則它們會關閉。定時序列中的Javascript動畫

這就是我的代碼的樣子(簡單的SVG只是一個演示模型)。它將手動添加類的元素,但不會復位它的定時器和無限重複,因爲我一直無法弄清楚如何清除班後,我將它們添加:

<!DOCTYPE html> 
<html> 
<head> 
    <style> 
     #line1, #line2, #line3, #line4, #line5, #line6 { 
      visiblity: hidden; 
     } 

     .showElement { 
      visibility: visible !important; 
     } 

     .growElement { 
      transform: scale(1.5) perspective(1px); 
     } 
    </style> 
</head> 

<body> 

<svg height="210" width="500"> 
    <line id="line1" class="first" x1="0" y1="0" x2="200" y2="200" style="stroke:red;stroke-width:2" /> 
    <line id="line2" class="first" x1="0" y1="0" x2="200" y2="300" style="stroke:orange;stroke-width:2" /> 
    <line id="line3" class="second" x1="0" y1="0" x2="200" y2="400" style="stroke:yellow;stroke-width:2" /> 
    <line id="line4" class="third" x1="0" y1="0" x2="200" y2="500" style="stroke:green;stroke-width:2" /> 
    <line id="line5" class="fourth" x1="0" y1="0" x2="200" y2="600" style="stroke:blue;stroke-width:2" /> 
    <line id="line6" class="fifth" x1="0" y1="0" x2="200" y2="700" style="stroke:purple;stroke-width:2" /> 
    <line id="line7" class="sixth" x1="0" y1="0" x2="200" y2="800" style="stroke:pink;stroke-width:2" /> 
</svg> 

<script> 
     document.addEventListener('DOMContentLoaded', function() { 

      setInterval(function() { 
       // create array of all the elements with the class 'first', loop over each one 
       // in this case, [' 
       var firstClass = document.getElementsByClassName("first"); 
       console.log(firstClass[0]); 
       console.log(firstClass[1]); 
       setInterval(function(){ 
        firstClass[0].classList.add("showElement"); 
        firstClass[1].classList.add("showElement"); 
       }, 1000); 

       var secondClass = document.getElementsByClassName("second"); 
       console.log(secondClass[0]); 
       console.log(secondClass[1]); 
       setInterval(function(){ 
        secondClass[0].classList.add("showElement"); 
        secondClass[1].classList.add("showElement"); 
       }, 2000); 

       var thirdClass = document.getElementsByClassName("third"); 
       console.log(thirdClass[0]); 
       setInterval(function(){ 
        thirdClass[0].classList.add("showElement"); 
       }, 3000); 

       var fourthClass = document.getElementsByClassName("fourth"); 
       console.log(fourthClass[0]); 
       setInterval(function(){ 
        fourthClass[0].classList.add("showElement"); 
       }, 4000); 
      }, 4000); 
    }); 
</script> 

</body> 
</html> 

我還需要圖瞭解如何一次添加多個類,僅需要一些步驟。例如,元素.first,.second和.third我想突然出現,所以我給他們.showElement,但元素.third我也想添加.growElement。我怎麼做?

這是我的Javascript代碼,不接受多個串聯的東西。它使用計數器,所以不像以前那樣粗糙。它的每個項目會仔細檢查該列表,並應用一種樣式:

<script> 
document.addEventListener('DOMContentLoaded', function() { 

    var connections = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']; 
    var i = 0; // index of the first item to show 


    setInterval(function(){ 
     console.log(connections[i]); 
     document.getElementsByClassName(connections[i]).classList.add("showElement"); 

     var counter = connections[i++]; // get the current item index and increment 
     if ((i == connections.length)) { 
      i = 0; // reset to first element if reach the end 
     } 
    }, 1000); 

}); 

注:我不是在尋找使用jQuery,只是純粹的JavaScript。這個動畫存在於一個單獨的頁面網站上,沒有其他帶有外部SVG的JavaScript,所以它必須存在於SVG文件中,而且我沒有看到安裝大型庫的許多要點。

但我正在研究可能的更好的方法來解決這個問題。有人告訴我這是'承諾'的工作。我現在正在研究Snap.svg,Q,When,WinJS和RSVP.js庫,如果你認爲它會更好,甚至jQuery,如果真的更容易,我會歡迎你的建議。

+0

只是澄清你怎麼想的那樣運行。 在一秒鐘內將showElement添加到className爲one的所有元素。 你是否希望showElement堅持,然後持續整整6秒鐘,將其從一切中刪除,並重復... 或僅爲最初的1秒然後刪除。 – Koborl

+0

是的,就像你描述的那樣。它可以持續整整6秒......這是我迄今爲止得到它的唯一方法。理想情況下,它只會持續1秒鐘然後被移除。 – cssidy

+0

在時間地圖這樣因此,最好你能告訴我應該如何運行 '1:1'' 2:2' '3:3' 在那裏秒過去:顯示元素,即哪些要素在什麼時間點顯示。 – Koborl

回答

0

這是一個小的清潔版本,它以1秒爲間隔進行動畫處理,並讓您更清楚地控制每個幀上發生的事情。

對於添加多個類,你可以添加任何你喜歡的全部一次,如:(和同樣去除See examplesclassList.Add('showElement', 'growElement')

動畫將在設定的框架回到0重複上一循環最後一幀(0,因爲幀計數器在switch語句之後遞增)。然後在第一幀中確保清除最後一幀的樣式。

<html> 
 
<head> 
 
    <style> 
 
     #line1, #line2, #line3, #line4, #line5, #line6, #line7 { 
 
     visibility: hidden; 
 
     transform: scale(1) perspective(1); 
 
     transition: .5s transform; 
 
     } 
 

 
     .showElement { 
 
      visibility: visible !important; 
 
     } 
 

 
     .growElement { 
 
      transform: scale(3.5) perspective(1px); 
 
     } 
 
    </style> 
 
</head> 
 

 
<body> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/plugins/CSSPlugin.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/easing/EasePack.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/TweenLite.min.js"></script> 
 
    
 
<svg height="210" width="500"> 
 
    <line id="line1" class="first" x1="0" y1="0" x2="200" y2="200" style="stroke:red;stroke-width:2" /> 
 
    <line id="line2" class="first" x1="0" y1="0" x2="200" y2="300" style="stroke:orange;stroke-width:2" /> 
 
    <line id="line3" class="second" x1="0" y1="0" x2="200" y2="400" style="stroke:yellow;stroke-width:2" /> 
 
    <line id="line4" class="third" x1="0" y1="0" x2="200" y2="500" style="stroke:green;stroke-width:2" /> 
 
    <line id="line5" class="fourth" x1="0" y1="0" x2="200" y2="600" style="stroke:blue;stroke-width:2" /> 
 
    <line id="line6" class="fifth" x1="0" y1="0" x2="200" y2="700" style="stroke:purple;stroke-width:2" /> 
 
    <line id="line7" class="sixth" x1="0" y1="0" x2="200" y2="800" style="stroke:pink;stroke-width:2" /> 
 
</svg> 
 

 
<script> 
 
       
 
var frame = 1; 
 

 
// create array of all the elements with the class 'first', loop over each one 
 
// in this case, [' 
 
var firstClass = document.getElementsByClassName("first"); 
 
var secondClass = document.getElementsByClassName("second"); 
 
var thirdClass = document.getElementsByClassName("third"); 
 
var fourthClass = document.getElementsByClassName("fourth"); 
 
var fifthClass = document.getElementsByClassName("fifth"); 
 
var sixthClass = document.getElementsByClassName("sixth"); 
 
var seventhClass = document.getElementsByClassName("seventh"); 
 

 
function hideUnusedClasses(p_classesArray) { 
 
    p_classesArray.forEach(function(array, index){ 
 
    for(var i=0;i<array.length;++i) { 
 
     array[i].classList.remove('showElement', 'growElement'); 
 
    } 
 
    }); 
 
} 
 

 
var interval = setInterval(function(){ 
 
    //console.log('Run every 1 second'); 
 
    switch(frame) { 
 
    case 1: 
 
     //console.log('frame', frame); 
 
     hideUnusedClasses([sixthClass]); 
 
     for(var i=0;i<firstClass.length;++i) { 
 
     firstClass[i].classList.add('showElement'); 
 
     } 
 
     break; 
 
    case 2: 
 
     //console.log('frame', frame); 
 
     //console.log('Delete 1 class'); 
 
     hideUnusedClasses([firstClass]); 
 
     for(var i=0;i<secondClass.length;++i) { 
 
     secondClass[i].classList.add('showElement'); 
 
     } 
 
     break; 
 
    case 3: 
 
     //console.log('frame', frame); 
 
     //console.log('Delete 2 class'); 
 
     hideUnusedClasses([secondClass]); 
 
     for(var i=0;i<thirdClass.length;++i) { 
 
     thirdClass[i].classList.add('showElement', 'growElement'); 
 
     } 
 
     break; 
 
    case 4: 
 
     //console.log('frame', frame); 
 
     hideUnusedClasses([thirdClass]); 
 
     //console.log('Delete 3 class'); 
 
     for(var i=0;i<fourthClass.length;++i) { 
 
     fourthClass[i].classList.add('showElement'); 
 
     } 
 
     break; 
 
    case 5: 
 
     //console.log('frame', frame); 
 
     hideUnusedClasses([fourthClass]); 
 
     for(var i=0;i<fifthClass.length;++i) { 
 
     fifthClass[i].classList.add('showElement'); 
 
     } 
 
     break; 
 
    case 6: 
 
     //console.log('frame', frame); 
 
     hideUnusedClasses([fifthClass]); 
 
     for(var i=0;i<sixthClass.length;++i) { 
 
     sixthClass[i].classList.add('showElement'); 
 
     } 
 
     frame = 0; 
 
     break; 
 
    default: 
 
     //console.log('all done'); 
 
     //clearInterval(interval); 
 
    } 
 
    ++frame; 
 
}, 1000); 
 
</script> 
 

 
</body> 
 
</html>

+0

,這正是我想要完成的。你知道如何把它放在一個無限循環?所以在最後一節課結束後,它會重複上一節課? – cssidy

+0

更新了代碼以使其循環 - 只需在動畫結束時重置框架即可。 – Ben

+0

非常感謝你教我如何做到這一點! :) – cssidy

0
var connections = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']; 

function showElements(i,duration){ 
    var elements = document.getElementsByClassName(connections[i]); 
    elements.map((e) => e.classList.add('showElement')) 
    setTimeout(()=>{ 
     //remove the .showElement after duration to display has elapsed 
     elements.map((e)=> e.classList.remove('showElement')) 
     //recursively calls the next animation to be played, resets at 6 
     showElements((i+1)%6,duration) 
    },duration)   
} 

不知道這是怎麼確切如何你想它,但它會添加showElement每個持續時間,然後將其刪除。

showElements(0,1000)