02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Staggering functions

To make text animate we need to have each character wrapped into an HTML object. To do that, we will cycle through our text and replace each character by will wrapping around a span tag on each. In this lesson, we will learn how to work with the staggered functions to animate a pile of things together using the methods: TimelineLite.staggerFrom, TimelineLite.staggerTo & TimelineLite.staggerFromTo.

Video Transcript

00:00As we progress through this chapter getting closer and closer to the end of it
00:06Recall from the last chapter
00:07We're talking about the ad that enabled us if we're adding more than one thing at a time to create a stagger effect
00:14And that's exactly what we're gonna look at because they actually have
00:17Specific functions dedicated to that as well the stagger from the stagger to and the stagger from to we're not gonna go into
00:24Every single one of them
00:25But instead of that we'll just see one of them because they kind of all work in the same type of logic
00:29So let's also tweak our animation and add a really cool little text animation really quickly and very very effectively
00:37Let's let's do it
00:39And the next stage and the next thing that I want to talk about is Sunday we met in the last course only
00:44We've met it as part of the add function, which is the capability of creating staggering effects now
00:51Previously we had to create the tweens themselves and then add them
00:55But you could actually also do it directly with the timeline light using the staggering functions
01:00There's the stagger from stagger to and stagger from to we're not gonna explore each and every one of them because they work in
01:07A very similar way
01:09So we're only gonna look at the stagger from and what I want to do is I want to create basically a stagger effect of
01:15All these characters animating each character separately having them fade in in parallel one after each other
01:22So to create that effect
01:23I'm gonna go ahead and what I want to do is I want to take that h2
01:26H1 that I have here and take each and every character and break it into their own
01:32HTML element in our case. I want them to have each their own span so I could animate each item separately
01:40so to do that, I'm gonna go ahead and
01:42Literally go to that text and I'm gonna just not load the images for a second until we get this all running
01:48Well, because we don't need all that noise
01:51Now what I want to do is I basically you want to take that initial text that we have and
01:56What I want to do is I basically want to I want to instead of slicing it. I want to basically split it and
02:03The way I want to split it
02:04I want to split it off of nothing basically an empty string meaning I want each character to be in its own
02:10element in this array
02:12Immediately after that I want to join it together because I want to use the glue that connects them with spans
02:18So really what will be is this will be in between the elements and in between the elements
02:23I want to close the span that was open because there's always a span that was open before the character and open up the next one
02:31Now this is not going to be good enough because I'm gonna have here a problem
02:34where I'm gonna have the first item is gonna have a won't have a starting span and
02:40The last item is not gonna have a closing span. That's easy enough to fix
02:44so if I go ahead here and just do a quick console that log just so we could see what I've just done and
02:49I'll just save this for a second. And if we look at this again, I'm just gonna open this up
02:54So we could look at it a little bit more deeply
02:55We'll see that we have here this missing opening span and we have here this missing closing span. So easy enough
03:02Let's go ahead and add that in. So all I really want to do is just add here also a starting span
03:08And
03:09Just make sure that it's wrapped around in a string and I also want to basically add to it a closing span again inside of a string
03:18span and
03:23There we go
03:23And now we have all the logic that we need all that's left for us to do is to substitute our text
03:29And I'm gonna undo that for a second. I want to substitute our text
03:32But this time around we want to update the actual HTML
03:36So I'm gonna go ahead here and update the HTML value and send in this new HTML
03:41We want to have inside of our span and if I go ahead and save this go back to my browser click on refresh
03:46And then look in my inspector itself to see the h1 here
03:51We'll see that each element is going to be inside their own span and that's all I really need to start working with the stagger
03:58functions
03:59So now that I have my elements I could literally just go ahead and now start talking to all these spans together
04:05sending them using jQuery, so I'm going to go ahead to my timeline and
04:10To my timeline and I'm gonna call the stagger
04:12From and again, you could use stagger to stagger from to the only difference is is that from two will have two parameters one?
04:20Object for the from one object to the two
04:22But the first parameter is an array of objects and because jQuery
04:26Automatically will return to me an array of my objects. I could literally just ask for the spans inside of my h1
04:33That will be the objects that I need the next thing. I'll tell it. How long do I want my animations to be?
04:39Third parameter will be what are my animations?
04:42So I could go ahead here and literally just set the thing that I wanted to start from in my case
04:47I could set it I want to start with an opacity of zero
04:50That's all I really had to do to have an animation that's working and each item is going to be
04:56Staggered so if I go ahead here and just save this and just go ahead here and click on refresh
05:01We should see that our items are gonna they're they're they're staggered
05:05But I don't have any rule that says what will be the the gap between them the gap between them right now is zero
05:10So they're all animating together, but literally all of them are on animating
05:14And that's exactly where my next property comes in is telling me how much of a gap do I want between each item?
05:21In my case, I want a zero dot two gap
05:24I want to zero dot two seconds gap between them and I also want to set my position which is the last
05:29Property which will set my position of one second
05:31I want to have for one second of nothing and then start this animation now with that said
05:37There's also one more thing
05:38I wanted to say is that if we're using the to everything here is similar
05:42We're just this object just refers to where we're going to and if I'm using the from to then I would just have your
05:47Another property right here another object right here. That would be the two as well
05:51But that's not where we're creating so we don't have to worry about that
05:54If I go ahead and save and go back into my application click on refresh
05:57We're gonna have a very very cool little animation going on a relatively clean animation going on as well
06:04And then last thing that I want to do even before we watch all the animation is I want my animations to begin
06:10Then the remainder animations to begin only after that happens
06:14But I don't have a problem with that and that is because I'm never I never said a hard code position of one or whatever
06:21It was always a relative position
06:22So I don't have any problem with loading my image at the same time because the image is the first animation that will happen
06:30Will only take place a half a second before
06:35Before actually man all my previous animations complete
06:39So if I go ahead here and save this go back to my application click on refresh
06:43We won't have here any conflict more over than that
06:45We're giving a little bit more time for the images to load and then when the copy is just about done to animate
06:51Then our images are gonna start coming in and that's exactly the type of effect
06:55We wanted to have now obviously we're still not incredibly happy with the background animation here
07:00But we will be by the end of this chapter
07:02So that's it about stagger functions, and I'm not gonna go through the stagger
07:08Stagger 2 and stagger from 2 because they literally work similarly
07:11And we've worked with them when we looked at the from 2 and the 2 and the from functions
07:17It's the same type of functionality only now we're sending in an array of objects if I find my object right here
07:23We're sending an array of objects instead of an object
07:27and then we have that control of having of the capability of defining basically the stagger effect and
07:34We set out to the position but literally is very similar to the ad only now
07:38We're behind the scenes creating those tween tween lights and we're using the stagger from so it's less code for us
07:44Less need for us to create different tweens alright, so that's it. That's it for this lecture

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!