2011-09-10 42 views
0

如何讓一個HTML元素像一個圓圈一樣行動,就好像它彈跳一樣,就像多次移動它一樣?有沒有可能只有CSS3?使用Css3的簡單動畫

回答

4

是的,你需要使用關鍵幀動畫來做到這一點。這裏有一個簡單的例子:

HTML:

<div class="bounce"></div> 

CSS:

@-webkit-keyframes hop { 
     from{ 
     -webkit-transform: translate(0px,0px); 
     } 


     to { 
     -webkit-transform: translate(0px,-30px); 
     } 
     } 

@-moz-keyframes hop { 
     from{ 
     -moz-transform: translate(0px,0px); 
     } 


     to { 
     -moz-transform: translate(0px,-30px); 
     } 
     } 

.bounce{ 
    display:block; 
    height:200px; 
    width:200px; 
    background:#ff6600; 
    border-radius:300px; 
    margin-top:100px; 
-webkit-animation-name: hop; 
    -webkit-animation-duration:.3s; 
    -webkit-animation-direction:alternate; 
    -webkit-animation-timing-function:linear; 
    -webkit-animation-delay:0s; 
    -webkit-animation-iteration-count:infinite; 
    -moz-animation-name: hop; 
    -moz-animation-duration:.3s; 
    -moz-animation-direction:alternate; 
    -moz-animation-timing-function:linear; 
    -moz-animation-delay:0s; 
    -moz-animation-iteration-count:infinite; 
} 

併爲您的觀賞樂趣小提琴:http://jsfiddle.net/hpBhf/1/

+0

幹得好! CSS3岩石! (但jQuery動畫仍然更加靈活,並且LOT更簡單):D – Abraham

+1

您不需要所有這些屬性。相反,使用動畫速記,它會減少10行。 –

+0

我知道,但我想如果有人不知道動畫的所有屬性,那麼如果他們完整地寫出來會更容易理解發生了什麼。 – DuMaurier