Staggered Animation in 3D: Exercise Preview
Staggered Animation in 3D: Exercise Preview
2D
Exercise
Exercise preview
Exercise overview
It’s easy to create super slick effects with minimal code with TweenMax.
In this exercise we’ll use the powerful staggerFrom() method to create a series
of animations on multiple elements. We’ll also dip our toes into some easy 3D
tricks along the way.
5. Reload the page as many times as you need to get a feel for the animation.
2. Take a look at the code from lines 15–42 to familiarize yourself with the DOM.
There are four elements that share a class of show. Each show is structured like
the following show for Dusty Shoes:
<div class="show">
<div class="pic"><img src="img/thumb-dusty-shoes.jpg"></div>
<h3>Dusty Shoes</h3>
<div>Sunday, June 8th</div>
<div>Show: 2PM $10</div>
<div class="button">Tickets</div>
</div>
NOTE: We will not have to edit any of the CSS for this project but if you’d like
to examine the linked CSS file, go into the Staggered Animation Folder, click on
CSS and open style.css.
3. Around lines 53–56 notice the following four JavaScript variables. We will tween
the jQuery object $shows, so it is shown in bold:
var $header = $("#header"),
$logo = $("#header img"),
$upcoming = $("#upcoming"),
$shows = $(".show");//selects every element with a class of show
Make sure you are aware that the $shows variable selects all four of the
elements with a class of show. There’s no need to tween each show individually.
4. There are also three basic TweenLite tweens that animate the $header, $logo
and $upcoming:
TweenLite.from($header, 0.5, {opacity:0});
TweenLite.from($logo, 0.5, {scale:0.5, rotation:-20, ease:Back.easeOut});
TweenLite.from($upcoming, 0.5, {opacity:0, delay:0.5});
They have already been coded for you because we are going to focus on
animating the $shows.
You’ll see the tweens for the header, logo, and upcoming shows. Let’s create a
complementary tween for the shows.
2. Save the file and preview the page in a browser. There is a delay of 1 second,
during which the header animation plays, and then all four shows fade in at the
same time, over a duration of 1 second.
4. Replace the deleted tween with the following new staggerFrom() tween, as
shown below in bold:
TweenLite.from($upcoming, 0.5, {opacity:0, delay:0.5});
5. Save the file and preview the page in a browser. The animation looks virtually
the same. We made the duration of the tween faster but, after the header
animates, all the $shows are still fading in simultaneously instead of in a
staggered manner. The staggerFrom() method needs an additional parameter.
6. In your text/code editor in the staggerFrom() method, add the following bold
parameter to your code like so:
TweenMax.staggerFrom($shows, 0.4, {opacity:0, delay:1}, 1);
This new parameter represents the stagger, the amount of time that transpires
between the start of each tween. Take a moment to note the syntax: the
parameter is always listed after the {properties} and it represents a value
in seconds.
7. Save the file and preview the page in a browser. The start time of each tween
is offset by one second. All four elements with a class of show have the same
staggered animation.
8. The stagger is a bit too long. In your text/code editor, change the stagger to
0.2, as shown below in bold:
TweenMax.staggerFrom($shows, 0.4, {opacity:0, delay:1}, 0.2);
9. Save the file and preview the page in a browser. The shows’ tweens overlap. It’s
a little too fast.
10. Let’s make the stagger the same as the duration. In your text/code editor,
change the stagger to 0.4, as shown below in bold:
TweenMax.staggerFrom($shows, 0.4, {opacity:0, delay:1}, 0.4);
11. Save the file and preview the page in a browser. Looks nice, but let’s add some
more pizazz to this tween!
2. Save the file and preview the page in a browser. While the animation is
different, it looks as if each element is just stretching vertically. This doesn’t
really look like a 3D effect. It lacks perspective.
CSSPlugin.defaultTransformPerspective = 100;
NOTE: Make sure to place the CSSPlugin code before the tween runs.
4. Take a moment to review the code you just typed. It’s important to point
out that this code will apply the same transformPerspective to every element
that will be animated in 3D space. If we had other elements we needed to be
animated in 3D space and wanted to set separate perspective values for just the
$shows, we could have entered the following code:
TweenLite.set($shows, {transformPerspective = 100});
5. Save the file and preview the page in a browser. Whoa, that sure worked!
There’s some serious 3D distortion going on. It’s a little much, though, because
we used a relatively low value of 100.
7. Save the file and preview the page in a browser. The distortion is less jarring to
the viewer and more effective overall.
We are essentially keeping the default horizontal value (center) but modifying
the vertical value to top.
2. Save the file and preview the page in a browser. Each element flips down from
the top! Great job, the tween is finished!
NOTE: In browsers that don’t support 3D transforms (IE9 and lower), the 3D
animations will simply be ignored but the opacity animations will still work fine.
TweenMax Methods