CSS3 Tricks: Falling Leaves Animation with CSS only
In today’s tutorial, we will create Falling leaves animation 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 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.
I also just prepared a Falling Snow CSS Animation for winter is coming. 🙂
We just developed our own free version of the Falling snow / leaves WordPress Plugin.
LIVE DEMO
Download Falling Leaves Source Files
SETTING UP THE LEAVES
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:
.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;
}
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.
.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;
}
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.
Unlimited WordPress Themes & Plugins From $16.5
IF YOU LIKE LIST ANIMATION YOU CAN ALSO CHECK OUR WORDPRESS THEME WITH RAIN ANIMATION
We are proud to present our fresh new WordPress theme: Raindrop. It is a multipurpose and flexible theme that offers you an almost unlimited variety of unique page layouts. We recently developed a free WordPress Blog Theme that I believe would be a nice start to your blogging journey.
SETTING UP THE LEAVES 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:
@-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);
}
}
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!
1 Comment
This is exactly what I was looking for 🙂 But this code doesn’t work for my image 🙁