Let the animations begin
It's time for us to do a quick rehearsal and set up for our tweening mission. In this lesson, we will create a very smooth modern animation for images. This base code will be used for the remainder of the chapter as we explore more advanced features. So let's get started.
Video Transcript
00:00Alright, we're done with our CSS, it's time for us to create a really cool animation.
00:04So let's set at it.
00:05It's actually a really long lesson because we're doing a lot in it, and it's in many
00:09ways a rehearsal, but also we're learning new things in the process.
00:13So it's going to be a very heavy lesson, but a very fun one.
00:16So let's get to it.
00:17I'm going to go ahead and set my background color to be a color that I pre-configured
00:22earlier that is one of our brand colors, which is our blue that you've probably met
00:27if you've taken any of our courses before.
00:31Once I set that, if I go back into my browser and click on refresh, we should have
00:34your nice blue background behind.
00:37And we're basically almost done.
00:40And before we could really, well, we're basically done with the CSS and we're
00:46ready to start animating.
00:48So let's just go back into our CSS and let's go back into our JavaScript.
00:53And it's time for us to, first step is I want to know when my image has
00:56completed loaded.
00:58So to know when my image completed loaded, I'm just going to go and approach
01:01that image now that it's been created.
01:03And I'm going to go ahead here and ask for my content image.
01:08And I'm just going to ask for the load event.
01:10So I'm going to call load and call a load callback.
01:13And this is a jQuery command, image load.
01:16And I'm going to go ahead and basically load that image up.
01:19Well basically have a callback when the image is loaded.
01:25And I'll just have your little console here, just to test that it's working.
01:30I was loaded.
01:33If I go ahead, save this, go back into my browser and click on refresh.
01:36We're going to see that in our console, we'll see that our image was loaded,
01:39which gives us an opportunity to animate in or fade in our image before it even begins.
01:45We could go ahead and do that very easily by just going ahead here and calling
01:49our tween light.
01:51And I'm just going to go to tween light and ask it to animate from.
01:56And starting off, I'm going to say, what do I want to animate?
01:58And actually, it's going to be this because our image is going to come back in the callback
02:03itself.
02:04And I'm going to say, how long do I want it to animate?
02:06I'll do it for half a second.
02:08And what do I want to animate?
02:09I want to animate a CSS attribute.
02:12And I want to animate the opacity.
02:14And I want to start it from zero.
02:16Now again, we're not going to talk about the how does the tween library work.
02:20We're assuming you know how it works.
02:22And if you don't, please do go to our intermediate course that we teach you everything about
02:27the animation library.
02:28So we're assuming that you know how to work with the library.
02:32And we're going to push it to the next level.
02:34But really, this is kind of like the rehearsal before we get to that position.
02:38So what we're doing here is we're animating from position from opacity as zero to full
02:42opacity with within the first half a second as soon as it's loaded.
02:47So if we go and do a nice quick little refresh, we'll see that our image now is going to kind
02:51of flash in probably a little bit too quickly.
02:54I personally would have it maybe animate in for maybe a little bit longer.
02:58So let me just do that really quickly.
03:00I'll have the image animate a little bit quicker.
03:03Now before, or slower more accurately, now before I'm going to wrap this up, I really
03:07want to have our image actually go through the whole process that I was talking about
03:11because this image is larger and we don't see the full image.
03:15So what I want to do next is I want to basically find out what is the size of my image.
03:20And to do that, I'm going to I'm going to want to approach the jQuery properties of the
03:25image because it'll be easier for me to retrieve the information.
03:28So I'm going to create here a variable called image and I'll just send inside our native
03:33DOM image that we're getting back, convert it into a jQuery image object, a jQuery
03:38object more accurately.
03:40And now there's a jQuery object that could very easily go to my console and just have
03:45it log out my image height and my image width.
03:54Now by the way, before I even trace this out, I'm going to make sure that I'm not
03:57going to approach the this anymore after I've wrapped it because tween light will
04:02automatically wrap our object into jQuery if it exists.
04:05So we're cutting down a little bit of processing time.
04:09So if I go ahead and save this and just add that semicolon, save it, go to the browser,
04:13click on refresh, we should see the size of the image as we're loading up.
04:17We could see the height and we could see the width.
04:19But really what we want to have happen is we want to have an animation that takes
04:23place within the content area.
04:26And when I say content area, it means this content section that we have here
04:30that has overflow that is hidden and a height that is 395 and width that is
04:36really inherited from above, which was 100%.
04:41So the next step for me that I need to do and want to do is basically configure
04:45what, how much do I want to animate?
04:47So to do that, I'm just going to go ahead here and just comment that out and I'm
04:51going to create a quick little variable called move up because really my
04:56intention, oh, and you know, by the way, before you even do this, let me just
04:59show you something.
05:00So even before we do this, if I go to my image and I'm going to go to my
05:03inspector and just find that image and if I go right into that image and I try
05:08to just add some properties to it and I go ahead here and I say I want to
05:12move the top to be at minus 20 pixels, you're going to see that nothing's
05:16really happening.
05:17It's not working.
05:18And the reason is, is because I have to set my properties of my CSS, I have to
05:24go ahead and tell my position itself what type of position it has.
05:28And for my sake, I don't want it to be absolute because I want it to live
05:31inside of this frame, but I actually want it to be relative.
05:35And once I configure it and set it to be relative, now the top position
05:39will work and I could start animating my element, having it go down based on
05:43how much I want it to go.
05:44But literally what I want to happen is I want it to animate all the
05:47way until it's basically done, but not more than that.
05:51Or in other words, I want it to go to minus 320 pixels, give or take.
05:55Now for me to do this and calculate it, what I could do is literally
05:58just approach the content area, ask it what its height and width is,
06:03and subtract the values from one another.
06:05So let's go ahead and do that.
06:07So I'm going to create that variable and I'm going to call that variable just as
06:11what it actually does, which is going to be move up, which is basically going to
06:15be the gap between our content area and our body.
06:19So I'm just going to go ahead again, create that content variable just so I
06:22have access to the jQuery properties.
06:27And then I'm going to go ahead and say, all right, what I want to do is I want
06:29to take my content height and I want to subtract from it the image height.
06:37Now what's going to happen with this, if I just do here a quick console again,
06:42and I'm just going to take that move up value instead of the height, and we
06:49click on refresh here, we're going to see that the value that we have coming
06:52back if I go to my console quickly is minus 324, which is exactly the value
06:57that I wanted.
06:58So I'm going to do the exact same thing also for the other side.
07:02And by the way, because we're working in pixels in CSS, I want to also make
07:05sure that I'm going to update and say, hey, by the way, this is a pixel value.
07:10So I'm going to go ahead here and add here plus PX, because we really want it
07:13to be pixels.
07:14And I'm going to go ahead and just duplicate this variable and put it up
07:19again one more time.
07:20But this time, we want to modify our left position.
07:24And I'm going to go for our width for this.
07:26And the same logic applies, exact same logic.
07:31And I could have here my move left.
07:33And if I just do a quick trace here, I'll be able to see that I'm getting that
07:36image, I'm getting the positions that I want to move my element to the left
07:40and the top in my console, where I have here a really small amount of
07:44animation from left to right, because my screen is really wide.
07:49So it's capturing all the size of the image.
07:52But from the height perspective, I have a lot more to travel.
07:56So if I go ahead here, and now that I have those values, I could go ahead
07:59and start an animation.
08:01So I'm going to go ahead here and add here another tween light two.
08:05And I could go ahead and say, I want to animate my image again.
08:08I want it to be about four seconds.
08:10The next thing that I want to animate is I want to animate the CSS properties.
08:15And what CSS properties I want to animate the top.
08:17And I want to move it up to the move up variable value, which was that
08:22value that we saw earlier.
08:23And to the left, we want to do the same thing for move left.
08:27Now just by doing this on its own, I'm going to automatically
08:29already get my animation going on.
08:33If we take a little peek at it, we're going to see that, first of all, by the
08:36way, one of our problems is that we have our fade in that's taking one second.
08:40And we really don't want this to overlap, because if we overlap it, we're just
08:44going to create a lot of noise in our animation.
08:48And by the way, with Rolla, our animation isn't working yet.
08:51Maybe because I didn't save it, let me go ahead and save it really quickly.
08:54And click on Refresh again.
08:57And oh, well, the problem is, is because we were showing you in the CSS
09:02that I have to change the CSS attribute to be relative, but I actually
09:06didn't do it.
09:06So I could do this with jQuery, or I could do it with TweenLight by going
09:11and configuring the values using the TweenLight set.
09:14But we've talked about that already in previous courses, so I'm not going
09:17to do that right here.
09:19Instead of that, I'm just going to go to my CSS, because this is something
09:21I'm going to need for all my images.
09:23And I'm just going to go for my image.
09:25Any image that's inside of content, and I'm just going to tell it,
09:29by the way, your position is going to be relative.
09:36And once I've set that and configured that, now I
09:38could go back to my animation, and it should now work.
09:41And we could see that our image is sliding in.
09:43Now, you'll notice probably, because we're not shifting a lot,
09:47and we're moving just a little bit to the left,
09:49there's a little bit of a jumpiness and jiggityness going on here.
09:53Part of it is because we're not using the round props plugin that
09:57will make sure that we don't go back and forth.
09:59And what's actually happening is we're animating a pixel back and forth
10:02here, or because we're sending a non-round number,
10:06it's creating a little jag.
10:09Instead of solving this problem, what I'm going to do
10:11is create a more dramatic amount of animation,
10:14so we're not moving only 10 pixels.
10:16So to solve that problem, what I want to do
10:18is I want to also add another from into our animation
10:21and basically expand our width initially.
10:24So that way, our width will be a lot bigger
10:26from our screen size, in our case,
10:28and give it a little bit more direction.
10:32So if I go ahead here, and I'm just
10:34going to go into my application here.
10:36For one is I want to make sure in my CSS
10:38that I want to use an ease that feels better for me.
10:41And personally, I've been playing around with it,
10:43and I kind of like the quint.ease out.
10:47And by the way, this is part of this ease,
10:50I believe, is part of the ease pack.
10:51So if you're only using tween light,
10:53just make sure you're also loading up the ease package
10:56for the quint.ease.
10:59And next thing I want to do is I want to add a slight delay.
11:03So I'm going to add your delay.
11:04So there's less of a competition between the fade
11:06and the animation.
11:07So there's a little bit of a break in between the two.
11:11I would even maybe create here a small little delay
11:13so we could see the image before it starts animating
11:16to capture the user's attention.
11:18Now, the next thing that I want to do
11:20is I want to create another from animation.
11:23But this time around,
11:24I want to add to that from animation a delay
11:28because I wanted to basically start after this transition.
11:31And I wanted to basically start after
11:35when these two are going on at the same time.
11:38And what I want to do is I'm going to have this animation
11:40only three seconds long.
11:42And I wanted to actually start from a much larger width.
11:48So I'm going to go into my CSS,
11:49I'm going to set my width to be 2000 pixels.
11:53Now what I'm doing here is I'm making,
11:55if we run through this really quickly,
11:57I'm fading in my image one second as soon as it's loaded up.
12:02A second and a half later for three seconds,
12:05I'm animating from a width of 2000 pixels
12:08to the width that it needs to be.
12:10And last but not least, I'm also animating my image
12:12so it's positioned by the end of all these animations
12:15to be in the bottom corner of the image
12:18so we could actually see the whole image
12:20throughout this process.
12:22So if I go ahead here and save this
12:24and go back into my application and click on refresh,
12:26we should see that our image is larger,
12:28we have there a second to absorb what's going on,
12:31and then our image continues
12:32and animates all the way in and completes.
12:36And that's it, that's it for this lecture
12:38where we kind of created a small little rehearsal
12:41to working with the Tween library,
12:44but in the process also learned a couple of new things
12:46integrated inside.
12:48In the next lesson, we're gonna continue
12:50from where we left off right here
12:52and taking a deeper look into
12:54how could we control the animations
12:56and really the conversation topic is gonna be
12:59the differences between static calls
13:01which we've been working so far
13:03throughout all of our courses with Tween Lite
13:05and moving to the Tween object itself.
13:08So we'll see you soon.
