Home | Advertise with us | Submit Community News | Hosting | Wordpress plugins
Header Image

CSS3 Tricks: Falling Leaves with CSS only

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.

LIVE DEMO

CSS3 Falling Leaves Preview!
             

Download Falling Leaves with CSS only source (110)

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:

<div class="fallingLeaves">
     <span></span>
</div>

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.

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:

@-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!

Download Falling Leaves with CSS only source (110)

NEVER MISS A PREMIUM TUTORIAL OR FREEBIE AGAIN. SIGN UP.

Error! Please correct marked fields. Subscription send successfully! Sending...

10 Comments for CSS3 Tricks: Falling Leaves with CSS only

  1. Connie
    January 22, 2012 at 9:03 am

    contradictory to this:

    “supported in webkit browsers (Chrome, Opera, latest versions of FF).”
    it doesn’t work with FF 9,0.1 (that’s the latest version)


    • PremiumCoding
      January 23, 2012 at 5:56 am

      PremiumCoding

      Hi,

      Yes you are right. I have corrected the post.

      best regards.


  2. Nathan
    January 23, 2012 at 7:02 am

    that is so awesome. I’m gonna use that for a Christmas card this year with falling snowflakes. sure eats animated gifs in PShop!


  3. 99designs vs Crowdspring
    January 23, 2012 at 9:24 am

    Such a useful script to make fun with CSS. Great to know some very nice tips about it. Thanks


  4. Beben Koben
    January 24, 2012 at 4:07 am

    I think there is an error code in the demo?
    Here @-moz-keyframes loader
    why use -webkit-transform
    !!!
    nice trick :)


  5. Sam Anderson
    January 25, 2012 at 11:27 am

    Whoa, brilliant giveaway. Nice to use this falling leaves script which i am searching for many days. Good one.


  6. Michael
    January 26, 2012 at 3:08 am

    Nice effect. I would worry about cross-browser compatibility though.


    • PremiumCoding
      January 26, 2012 at 6:51 am

      PremiumCoding

      Hi,

      Yes, this tutorial is simply for showing what can be achieved with CSS3 and only works in webkit browsers like Chrome. But support for this kind of effects will ofcourse increase in the future.

      best regards,
      Team PremiumCoding


  7. chuk
    February 1, 2012 at 3:54 pm

    does it work on IE??


    • PremiumCoding
      February 2, 2012 at 6:37 am

      PremiumCoding

      Hi,

      No, IE doesn’t support this kind of CSS3, only webkit browsers do.

      best regards,
      Team PremiumCoding



Leave a Comment




*

Array
Socialize with Us!
  • Facebook
  • RSS
  • Twitter
Contact Details
Company:PremiumCoding
Email:info@premiumcoding.com
Total Downloads
48234