css3-falling-leaves-effect

CSS3 Tricks: Falling Leaves with CSS only

January 20, 2012 by PremiumCoding - 16 Comments

In today’s tutorial we will create Falling leaves effect without any help of javascript. When I started playing with CSS3 I never thought that I could make such fascinating animations with it. I made falling leaves animation in both Flash (used in our WordPress Falling Leaves plugin) and Javascript but I never thought that it is possible to make it with CSS3 only. While it is not as cool as the Flash one, it is still fairly decent and it uses less CPU. I recommend that you download the source files first and check the live demo. If you’d like to print these instructions as a reference, ordering new ink online is the easiest route – the best ink providers also offer toner cartridge recycling free of charge to all customers.

LIVE DEMO




Download Falling Leaves with CSS only source (1986)

SETTING UP THE LEAVES

First we have to add the class for leaf in our html file. Simply add the following code into index.html:
[cc lang="html"]

[/cc]
We add one instance of the leaf with the <span></span> tag. If you wish to add more leaves simply multiply this tag. Now create a file style.css and add the design for the class created above:
[cc lang="css"]
.fallingLeaves span {
display: inline-block;
width: 80px;
height: 80px;
margin: -280px 40px 54px -34px;

background:url(“leaf.png”);

-webkit-animation: fallingLeaves 10s infinite linear;
-moz-animation: fallingLeaves 10s infinite linear;
}
[/cc]
Add the leaf here as a background for each instance of span that we defined earlier. Size and initial position of the leaves is set here. Webkit animation is actually pretty simple. We set which class to animate (fallingLeaves) and set the falling time to 10 seconds. We want the animation to continuously repeat itself so we will set it to infinite. Eeasing is set to linear to achieve a smooth falling effect.

Before we get to the animation effect we have to somehow randomize the leaves so they don’t all start falling at the same time. I used the nth-child method to define several sequences of leaves with different animation delays.
[cc lang="css"]
.fallingLeaves span:nth-child(5n+5) {
-webkit-animation-delay: 1s;
-moz-animation-delay: 1s;
}

.fallingLeaves span:nth-child(3n+2) {
-webkit-animation-delay: 1.5s;
-moz-animation-delay: 1.5s;
}

.fallingLeaves span:nth-child(2n+5) {
-webkit-animation-delay: 1.7s;
-moz-animation-delay: 1.7s;
}
[/cc]
These are just three examples, I added several more sequences in the live example provided above. Download the source files to see them all. First sequence is set to 5n+5 which means leaves number 5, 10, 15,… will start falling with the delay of 1 second. Second sequence is set to 3n + 2 which means leaves 2,5, 8, 11,… will start falling with the delay of 1.5 second. Note that some sequences may overlap with each other but it’s not a big deal, the css will always take the last setting and use it for the animation.

SETTING UP THE ANIMATION

All we have to add now is the animation effect. It is actually pretty simple. Add the following code to your style.css file:
[cc lang="css"]
@-webkit-keyframes fallingLeaves {
0% {
opacity: 1;
-webkit-transform: translate(0, 0px) rotateZ(0deg);
}
75% {
opacity: 1;
-webkit-transform: translate(100px, 600px) rotateZ(270deg);
}
100% {
opacity: 0;
-webkit-transform: translate(150px, 800px) rotateZ(360deg);
}
}
[/cc]
As you can see we divided our animation into three parts (state at 0%, at 75% and at 100%). Animation starts with x and y coordinates set to 0px and rotation along Z axis also set to 0. This is the initial position of the animation. After the animation starts it will start moving towards position set in the second part (75%). Leaves will move slightly to the right (x coordinate is set to 100px) and down (y coordinate is set to 600px). It will also rotate for 3/4 of the full circle (270deg). Leaves will still be fully visible (opacity is set to 1). Opacity is the reason why we added the second part of animation anyway. It allows us to slowly fade the leaves to invisible after they reach a certain point. When leaves reach the ending point (100%) they are instantly moved back up where they start animating again. And we are done. The implementation is pretty simple and straightforward and you get a decent falling leaves effect in no time.

CONCLUSION

CSS3 animations can be very powerful and diverse and even animation of multiple objects is a piece of cake. Please note that these animations are only supported in webkit browsers (Chrome, Opera). In our future tips&tricks posts I will show you how to make gradients, apply animations to images and much more so stay tuned!

Download Falling Leaves with CSS only source (1986)