2011-09-10 56 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