2014-12-07 74 views
0

我需要在javascript中製作一個增長和減少的圓圈。 我的想法是使用格與兩個值之間的Ocillate函數javascript

border-radius : 50% 

要得到一個圓。我需要每隔[x]秒將其從0.2縮放到1。

它在5秒內從0.2增長到1,然後在5秒內從1減少到0.2。運動再次開始。 我想我必須使用sin或cos函數,但我不知道如何根據時間獲得這個間隔。

我需要它與JavaScript計時函數耦合,以便當我提出一個計時器時,動畫開始,當我暫停它時,它暫停動畫。

感謝您的諮詢!

+0

我會看看使用類和CSS動畫或鏈接回調,以便當函數完成所有處理時,它使用反參數調用自身。 – 2014-12-07 13:51:50

回答

1

你可以用CSS3動畫做到這一點。看看這個例子,並改變它,直到它完全按照你的想法。

#circle { 
 
    -webkit-animation: oscillate 10s infinite; 
 
    animation: oscillate 10s infinite; 
 
    border: 1px solid black; 
 
    height: 100px; 
 
    width: 100px; 
 
} 
 
@-webkit-keyframes oscillate { 
 
    0% { border-radius: 20%; } 
 
    50% { border-radius: 100%; } 
 
    100% { border-radius: 20%; } 
 
} 
 
@keyframes oscillate { 
 
    0% { border-radius: 20%; } 
 
    50% { border-radius: 100%; } 
 
    100% { border-radius: 20%; } 
 
}
<div id="circle">Hi</div>

+0

我需要它與一個javascript定時器鏈接,以便我可以暫停/恢復動畫,所以CSS只是不是解決方案即時尋找:/ – arlg 2014-12-10 09:58:41

1

CSS動畫這可能是更好的選擇的又一個例子那麼的javascript:

.circle { 
 
    background: coral; 
 
    height: 100px; 
 
    width: 100px; 
 
    border-radius: 50%; 
 
    -webkit-animation: pulse 1s ease infinite alternate; 
 
    animation: pulse 1s ease infinite alternate; 
 
} 
 
@-webkit-keyframes pulse { 
 
    from { 
 
     -webkit-transform: scale(1); 
 
    } 
 
    to { 
 
     -webkit-transform: scale(.2); 
 
    } 
 
}
<div class="circle"></div>

對於Firefox,你將需要添加prefixless規則

@keyframes pulse { 
    from { transform: scale(1); } 
    to { transform: scale(.2); } 
} 
+0

感謝您的答案,但我需要它是JavaScript,因爲我有一個計時器有一個暫停/恢復方法 – arlg 2014-12-10 10:02:10

0

使用無限的CSS3動畫集。檢查這fiddle

@-webkit-keyframes mymove { 
    0%, 100% { -webkit-transform: scale(1); } 
    50% { -webkit-transform: scale(0.2); } 
} 
@keyframes mymove { 
    0%, 100% { transform: scale(1); } 
    50% { transform: scale(0.2); } 

} 
div { 
    width: 100px; 
    height: 100px; 
    background: red; 
    position: relative; 
    -webkit-animation: mymove 5s infinite; /* Chrome, Safari, Opera */ 
    animation: mymove 5s infinite; 
    border-radius:60px; 
} 
+0

我需要它與JavaScript計時器連接,以便我可以暫停/恢復動畫,所以CSS只是不是解決方案即時尋找:/謝謝反正 – arlg 2014-12-10 09:58:05