Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add random to a css3 animation loop

Each time a css3 animation loops I'd like to change a variable in the style.

For example the following css drops a group of parachutes from the top to the bottom of the screen:

@-webkit-keyframes fall {
  0% { top: -300px; opacity: 100; }
  50% { opacity: 100; }
  100% { top: 760px; opacity: 0; }
}

#parachute_wrap {
  -webkit-animation-name: fall;
  -webkit-animation-iteration-count: infinite; 
  -webkit-animation-duration: 70s;
  -webkit-animation-timing-function: linear;
}

Ideally however, at the end of each loop I'd like to throw in a random X position.

The default style of the group is:

#parachute_wrap {
  position: absolute;
  top: -300px;
  left: 50%;
  margin-left: 140px;
}

So after 70 seconds, I would like to change the margin-left or left attribute anywhere between -200px and 200px.

I understand I could do something like this with jquery and everytime():

$('#parachute_wrap').everyTime ( 70000, function (){
    $("#parachute_wrap").css({left: Math.floor(Math.random() * 51) + 5 + '%'},0);
});

But is there a way to tie the css loop to js to do this? i.e. is there a return call of any kind which js can listen to for perfect syncing with the animation? I fear that if I use JS on a 70s timer whats going to happen is the timing is going to not sync right, and half way through the css animation the js will do its timed loop and the parachutes are going to jump around the place half way through the animation.

How would you approach this?

like image 282
willdanceforfun Avatar asked May 15 '12 23:05

willdanceforfun


People also ask

Can you generate a random number in CSS?

External JavaScript Let me get that out of the way first — CSS has no built-in “random” function, no Math. random() equivalent and no way to generate a random number or a random color at all.

How to add animation-delay CSS?

The CSS animation-delay property has the following syntax: animation-delay: [time] | initial | inherit; As you can see, there are three possible values: time, initial, and inherit. The first option is [time], which is the number of seconds or milliseconds before the animation starts.

What property exist to add delay for animation?

The animation-delay CSS property specifies the amount of time to wait from applying the animation to an element before beginning to perform the animation. The animation can start later, immediately from its beginning, or immediately and partway through the animation.


2 Answers

CSS3 animations have an animationEnd event fired when the animation completes.

You can bind that event to your animated element thus triggering that left change at the end of each animation:

$('#parachute_wrap').on('webkitAnimationEnd mozAnimationEnd msAnimationEnd oAnimationEnd animationEnd', function(e) {
    $(this).css({left: Math.floor(Math.random() * 51) + 5 + '%'},0);
});

This way, you know exactly when the animation ends and the left change is applied correctly.

like image 93
Zuul Avatar answered Oct 20 '22 09:10

Zuul


There is also an animationiteration event fired at the start of every new animation iteration, i.e. every iteration except the first.

$('#parachute_wrap').on('animationiteration webkitAnimationIteration oanimationiteration MSAnimationIteration', function(e) {
    $(this).css({left: Math.floor(Math.random() * 51) + 5 + '%'},0);
});

http://www.w3.org/TR/css3-animations/#animationiteration

like image 22
maiis Avatar answered Oct 20 '22 09:10

maiis