Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Sync CSS Animations Across Multiple Elements?

I have two elements on a page that I'd like to animate via CSS (specifically, -webkit-animation). The animation itself simply bounces an element up and down. One element is always shown and bouncing, the other is not animated until mouse-over (hover).

My question is: Is it possible to sync (have both elements reach their apex at the same time, etc) the animation across both elements regardless of when the 2nd element's animation is started?

Here's my HTML:

<div id="bouncy01">Drip</div> <div id="bouncy02">droP</div> 

and my CSS:

@-webkit-keyframes bounce {     0% {-webkit-transform: translateY(0px);}     25% {-webkit-transform: translateY(-2px);}     50% {-webkit-transform: translateY(-4px);}     75% {-webkit-transform: translateY(-2px);}     100% {-webkit-transform: translateY(0px);} } #bouncy01, #bouncy02 {     margin:10px;     float: left;     background: #ff0000;     padding: 10px;     border: 1px solid #777; } #bouncy01 {     -webkit-animation:bounce 0.25s ease-in-out infinite alternate; } #bouncy02 {     background: #ffff00; } #bouncy02:hover {     -webkit-animation:bounce 0.25s ease-in-out infinite alternate; } 

and finally, a working demo of the issue: http://jsfiddle.net/7ZLmq/2/

(to see the problem, mouse-over the yellow block)

like image 875
busticated Avatar asked Jan 29 '11 19:01

busticated


People also ask

Can an element have multiple animations CSS?

While it is true that you cannot play two transform animations at the same time (because one transform would overwrite the other), it is not correct to say "You cannot play two animations since the attribute can be defined only once." See the accepted answer about using comma-separated values with animations.


1 Answers

I don't think its possible natively, but you can actually hack similar functionality by using a bouncing wrapper and some position altering

html:

<div id="bouncywrap">     <div id="bouncy01">Drip</div>     <div id="bouncy02">droP</div> <div> 

CSS:

@-webkit-keyframes bounce {     0% { padding-top:1px;} /* using padding as it does not affect position:relative of sublinks  * using 0 instead of 0 b/c of a box-model issue,  * on kids wiht margin, but parents without margin, just try out  */     50% { padding-top:5px;} /*desired value +1*/     100% { padding-top:1px;} }  #bouncy01, #bouncy02 {     margin:10px;     background: #ff0000;     padding: 10px;     border: 1px solid #777;     width:30px;        position:absolute; } #bouncywrap {     -webkit-animation:bounce 0.125s ease-in-out infinite;     position:relative;     width:140px;     height:50px; /*    background:grey; /*debug*/ } #bouncy02 {     background: #ffff00;     left:60px;     top:2px; /*half of desired value, just a fix for the optic*/ } #bouncy02:hover {     position:relative; /*here happens the magic*/     top:0px; } 

demo http://jsfiddle.net/A92pU/1/

like image 145
Valerij Avatar answered Sep 30 '22 16:09

Valerij