Functions Galore
What better way is there to improve your craft than to jump deep into the ocean without any buoy. That's exactly what we're going to do in this lecture. You had a dry run with functions and saw more or less how they work, now we're taking them to the edge.
For the remainder of the chapter we will be pushing our functions to their limit as we create awesome animations.
Video Transcript
00:00If you asked me what's the most important thing in everything in programming, it would be functions because functions are the heart of everything.
00:08That's what really makes it possible for you to create incredibly complicated things.
00:13So although we have a lot more things to learn about, I wanted to take a break from learning new things for a while, although we will be learning new things in the process.
00:21But what I want to do is I want us to play with functions and create some really rad animations.
00:26So let's get started.
00:29So first step first, let's see where we're at.
00:31We have our image loading in.
00:33And what I want to do is I want to create another function that will basically take our image and slide it to the bottom.
00:39So let's try to figure out how to do it.
00:40And again, if we just select our item, inspect in Firebug, we can go into the style area itself and then figure out, okay, we want to move actually our bottom to go to animate into minus 190 degrees, 190 pixels.
00:54So let's go ahead and animate the bottom to go to 190 minus.
01:00So let's say this, I'm going to create another function just by duplicating the function I've created.
01:19And I'm just going to give it a unique name because we can't have the same name.
01:22So we'll call this image animate image out, animate image out.
01:29And I'm going to go ahead to the exact same image, the exact same amount of time.
01:33Instead of opacity, what I want to animate again, if we go into our element, we want to animate our bottom and want to go to minus 190 pixels.
01:41So that's going to be bottom.
01:43And we want to animate instead of that value, we want to send your 180 PX.
01:48And notice how I'm wrapping it with the string because there's copy inside.
01:51So this is a string value.
01:53Oh, we wanted minus 180 PX.
01:55The last thing that's left for us to do is or trigger this immediately, which is not what we want to do or just call the delayed call like we've used earlier.
02:02So I'm going to go ahead and just copy the same delayed call that we've created earlier, sending the first parameter when, which we want to tap in two seconds in.
02:11And then what function do we want to call?
02:13We want to call the animate image out that we've just created.
02:17All right.
02:18So let's just walk through this before we even play it out.
02:21So first thing first is everything is going to become opacity zero.
02:25It's going to happen immediately once our application loads.
02:29Next thing we call the animate in which animates inside the two text elements to opacity of one.
02:35The next step is we're using the tween max delayed call and one second in we're going to call the animate in image, which will animate in our image into opacity of one.
02:46Last but not least is we're going to call the animate image out after two seconds again using the tween max delayed call.
02:53So if I save this and everything works appropriately and I click on refresh, we're going to see our image fades in and then it slides right out.
03:02So let's go and continue because we said we wanted to switch the image.
03:06So I'm going to go ahead and just make this even three seconds in and I want to create another function, which will be swap image.
03:13So I'm going to create a function and I'm going to call that function swap image and swap image is going to use the same tween to mine.
03:26No, it's not going to use a tween.
03:27It's just going to swap the image, right?
03:29So to swap the image, I'm going to actually use a jQuery function, which is again, it's called the dollar function and into it.
03:35I want to send what item because I want to turn it into an object.
03:38And in this case, I want to get my image.
03:40So this will return to me my image with a few extra functions that are only available once we add the jQuery commands, making sure we don't have to use browser combat abilities and whatever.
03:51So literally dollar sign is to trigger jQuery.
03:54And then we say, hey, take my my image and just turn it into an object so I could access it and use some properties that you add to it, which will make my life easier.
04:03In this case, it will have a property, a function called ATTR.
04:07Now, I'm talking about functions properties for now.
04:09Just assume they're all the same thing.
04:11Now, the property or function ATTR, which stands for attribute, accepts two parameters.
04:17One is the attribute that we want to change.
04:19In this case, we want to change the source.
04:21So I'm going to put here the string source.
04:23And the second property is what do we want it to turn into?
04:27So again, I'm just going to copy the source itself.
04:30But really, what we want to do is we want to switch our source from slide one to slide two.
04:36And if I go ahead, save this.
04:38Now, if I run this just like this, we're not going to see anything because we're never calling swap image.
04:43So what I'm going to do right now is I'm going to go right back up there and call again the delayed call.
04:48This time around, let's give it a delay of four seconds because we want it to happen after this animation completes.
04:54And then we're going to call swap image.
04:57Now, we're going to have a problem because our element is going to be out of the range of our view.
05:02So even though the image will swap, we'll never see it.
05:05So to solve that problem, I'm going to go into swap image and I'm going to animate our image in by calling the animate in image.
05:13So literally, look what I'm doing.
05:15I'm now calling in a function that I've created earlier, calling it again from within a new function that I've created.
05:20This is the first time we do that.
05:22We're basically nesting inside another call.
05:24So when I call swap image, which is going to happen four seconds in, it's going to swap our image immediately.
05:31And then it's going to call animate image in, which is going to set my opacity to one, which in our case is not good enough because we're still going to be where we will already be an opacity one.
05:42But we're going to be out of the zone of our screen.
05:45So I'm going to copy this tween and I'm going to add to it one more tween.
05:49And this time around, I'm going to add into it the tween to bottom.
05:52And I'm going to set that to be at zero P X for zero pixels to bring it back to where it originally was.
06:00Now, notice what I'm doing here.
06:16I'm using two tween.
06:17I'm animating the opacity and I'm animating the bottom for as a side note, forever.
06:22You who already know how to work between Max, we're doing this in a little bit longer process than we should because we don't want to add complexity at this stage to keep things simple.
06:31All right.
06:32So with that said, and for those of us that don't know that, just ignore what I just said.
06:37What is really critical here for us to understand is even when we call animate in image the first time around after a second, it'll just move our bottom to pixel zero, which is already at zero.
06:48So basically it won't do anything, but it still is going to put our opacity into one.
06:52Now, later on, when we call swap image and then swap image is going to call animate in when our visibility, when our opacity is already at one.
07:01But our position is at minus 180.
07:03It's literally not going to change our opacity because our opacity stayed the same, but it's going to actually push our animation back to its original position.
07:11So let's save this and go back into our browser and look at this magic work in action.
07:16So our text will load.
07:17Our image will fade in.
07:19Our image will go down and then come back up as a completely different image.
07:23So if that doesn't excite you, I have no idea what would.
07:27So stop watching new videos.
07:29Take a break now, create a really wicked animation and drop a link to it.
07:34If you have a website or drop your source code, if you're working with the same code that we have right in the comment section, because I would love to see your work and what animation combination you created and your homework right now.
07:45My homework for you is use four functions, create your own four unique functions, delete what we just did, come up with your own four functions that would create some sort of dynamic, customizable animation.
07:55Using any CSS property you want, using anything you want really to animate.
08:00You could use the top, bottom, left, right, whatever you want.
08:02Just figure it out and we'll see you in the next video, but hopefully I'll see you even before that in the comment section when you're going to show me what you did.
08:09In the next video, we're going to continue working with functions.
08:12Continue working with the functions we just created, but we're going to go and visit them one more time to try to figure out if we could improve them a little bit more by adding parameters into them.
08:22So we're going to see you in the next lecture when we start digging into the world of parameters.
