Stop hovering over me
In the last lesson we learned how to create CSS animations using TweenLite and the CSSPlugin. In this lesson we're going to take our animations and make them more interactive, with the help of the jQuery method hover(). By the end of this lesson, we will have a rollover effect that will wash over our components.
Video Transcript
00:00It's time for us to add interactivity and that means we want to add a rollover state and a roll out state
00:07But really any interactivity it's time for us to move into that domain. So to do that, we're gonna leverage jQuery's
00:14Features and we're gonna explore the hover function which method actually and with it
00:20We're gonna see how do we add a little bit more interactivity and again working with our CSS
00:24So let's get back into it with our mission
00:26So we we got so far into that level where we had that initial very cute animation
00:32I think thank you very much and the next step although I should thank G sap because they did most of the work
00:38The next step is I want to create this rollover effect and for that we're gonna turn over to
00:43jQuery to save us some time
00:45So let's go ahead because we are loading jQuery after all and what I'm gonna do is I'm just gonna call the jQuery
00:51Function and then wrap it around what we actually want to animate in this case
00:56I'm I want to get the actual elements
00:58So I want to get those Li elements because those are the items that I want to animate
01:04The next step is I want to call the hover function and the hover function basically or method really more accurately
01:11Excepts two parameters your rollover and your roll out. So I'll just create a really quick function. Let's call this on news
01:18Because this is gonna be for our news section on our site on news over on
01:24News out
01:25So that'll be a great way for me to indicate what they are and I'll just go ahead here and just create those functions
01:31really quickly on
01:38News over
01:40And I'll just copy this and duplicate that function
01:46Once over and make sure that I rename one of them to be the news out. Perfect
01:51All that's left for me to do. Well, I have a few more things left to do
01:54But let's do your quick console to see that it's working. I always like testing things out. So
02:00In our console saves time
02:02So when you create something it just takes three seconds and although you might think that this takes longer
02:07But actually when you test things out as you're going
02:10And make sure that you're making less mistakes and whenever you have a mistake
02:13It's easier for you to figure out what it is because you just tested it in your browser
02:18So I'm gonna go ahead there click on refresh and I should see her in out in out and that's exactly what I wanted
02:24And it's definitely doing what I want and now it's time for us to learn something really new
02:29Maybe some of you know it maybe some of you don't
02:32But what I want to talk about is scope and we talked about scope in our last course
02:36We talked about scope also in our last chapter, but let's take it a step deeper right now
02:42I'm inside of this function, but jQuery by default treats this function
02:47As a method it makes sure that it's this will be the actual item that is being hovered on
02:53So literally if I just go ahead and type here this it's gonna this value is actually gonna be this object that I got back
03:00From jQuery, which is really good news
03:03So if I save this go back into my application click on refresh
03:06We should see there that we should be getting back our item our actual HTML item
03:11It's not wrapped around in side of the jQuery
03:15So I'm gonna have to wrap it around so I'll have access to the methods because that's what I need
03:19So now that I see that I'm getting that item and and and again
03:24This is a really important thing because and if you just did this and just any regular function
03:28You would definitely not get that specific item
03:30So this is a really great workaround a really great feature that
03:34jQuery has that makes our life really easy because now I don't have to figure out who this is
03:39I know who this is all the time
03:41So now that I know who this is
03:43I'm just gonna get rid of that console what I want to do next is I literally want to find from within this
03:50Li item inside of this Li item what I really want to get to is I want to get to the span with this
03:55Class of BG and I know that there's only one item with the class of BG. So I'm gonna use another jQuery
04:03Fantastic method that which is called find and with our find I could literally tell you hey only within this item that we've just wrapped
04:11Around could you find me the dot BG and get me that dot BG?
04:15And now that I have access to the dot BG, I could now start animating it which is wicked
04:21So what I'm gonna do next is basically create that tween
04:25so I'm gonna go ahead here and just create this tween really quickly tween light and I want to to it and
04:31I'm gonna send it into it our item that we've just discovered and our second parameter
04:37We want is gonna take one second to animate and last but not least
04:40We also want to say to it. Where do we want to go?
04:43What do we want to animate in our case? We want to animate that padding top again?
04:47And again, we're gonna need to have access to that to that
04:52CSS attribute and notice initially what I'm gonna do
04:56I'm gonna do it the long way and then I'm gonna show you well not necessarily long way
05:00But the shorthand that looks probably messy, but not only does it look messy messy
05:04It's also gonna be a little bit less efficient
05:07so by calling this function again and then calling the CSS and
05:11Then calling its height what I'm actually doing is I'm calling this function twice
05:17Now it also looks a little bit messy
05:19So what I could do is just create here a quick variable and then just call this variable
05:23once you use something more than once is a good idea to create a variable and as this
05:27Variable scope is inside of this method only when this method is running the variable would exist
05:32So it's not gonna add anything on to our system
05:35It's not gonna create more if we have a lot of these it won't really matter
05:39So I could just create this var and I'll just call it my li
05:43And I'll call my li
05:45Basically wrapping around that this that we got there and then I could approach it directly and although this is gonna look very similar
05:51What we're actually doing is we're cutting down the amount of times that we're calling methods when we don't need them now
05:57You might be thinking hey
05:58Why don't I take this height and put it as a global variable which would be a noble cause?
06:03But if you do do that, what are the problems you're going to encounter with that?
06:08Is that you're adding more variables to your global scope and we don't really want to do that
06:14So maybe theoretically we could have gone to initially and go through every single li and make an accessible
06:20Variable that would be called height. But in our case, we're not doing that
06:25And this is good enough for us. So now that we created that I'm gonna go ahead and
06:30Let's just save this and make sure that it actually works because that's always a good test
06:35Test out and see what works before you move on
06:38So if we roll over after the item is done and I roll over it's gonna roll over beautifully without any problem
06:45Now the next thing I want to do is actually set up that roll out and the roll out is going to be even easier
06:50Because it's basically exactly the same thing and we're just changing that padding top to um, basically zero pixels
06:57So if I go right and head here and I could set that
07:01CSS value instead to just be here
07:05See to be zero px
07:10And that's it we just created our animation now before we wrap up this lesson
07:14I want to have another small talk about css before we move on to the next topic
07:20So let's just see that it works and it sure does and the animation stops and this is beautiful
07:25If we're rolled out before it's over then the animation stops if we roll out quickly here
07:30Then it creates this really nice splash effect
07:32I personally think it's a little bit too slow. So what I would probably want is maybe create the animation. That's a little bit
07:39A little bit faster
07:40But i'm not going to play around and tweak with this too much because I want to give you the chance to create
07:45A really awesome animation of your own because we might actually feature it and actually use it. So let's go ahead and just
07:51Just test this out just really quickly. We'll see that now we're going in a lot faster
07:56And we're going out slower, which is totally fine
07:59So that way it's kind of like, uh, there's a little bit more depth a little bit more personality to our animation
08:06All right before I wrap this up. I want to talk about one thing now
08:10Sometimes you might and this is you know, like sometimes, uh, well, let me just say this in general once you load up your
08:18css and you add in that css plugin automatically you could animate css items directly most of them
08:25But what you probably would want to do in general and it's not a bad idea to do this all the time
08:31Is you want to wrap up your actual your css attributes in another object by just saying css
08:37And then wrapping it inside there. Now, the reason we want to do that is very simple
08:44Green sock automatically whenever it sees padding top
08:48It's going to first try to see if it's something in the DOM element that it could animate
08:51If it can't find it there, then it's going to approach the css
08:55So literally you have here an extra step that might not be very noticeable, but this extra step could
09:01Become a lot less efficient
09:03So if you have a lot of things going on at the same time a lot of animations going on at the same time
09:08This animation would be more efficient because we're not we're we're telling j we're telling
09:13Gsep. Hey, this is css attribute that you're animating
09:16So you could skip through that process of figuring out if it's a css animation or not
09:21You could choose if you want to use that css item or not
09:25Most css attributes, you know, it really just depends on the version, but there i've noticed here and they're small bugs sometimes
09:32Where it doesn't figure out where?
09:35That it's actually a css attribute
09:37So you're actually helping the you're helping gsap
09:41And just typing a couple of extra words is not that big of a deal
09:45And that will actually save you a lot of headaches when it comes down to small little bugs
09:49When you just approach things directly and just say always hey, um, we're animating actually the css attribute
09:55And look how easy we updated that it's a little bit more code, but nothing too drastic
10:01And by the way, you like I personally am a fan of when you have these type of animations that repeat themselves a lot
10:07Is to create just a dedicated function because then we could reuse that function and if in the future
10:13We want to make a few tweaks that are
10:16For everything then it would work for it and I suggest you do it
10:19I'm not going to do that just because um, you know for the sake of time in the course
10:22But I personally recommend is, you know
10:24try to
10:26Releverage functions as much as you can and reuse functions as much as you can and create functions
10:31Even if you're over creating functions, it's fine in the beginning over time. You'll find that right balance
10:36Of how many functions you want to be able to do the things you want to do
10:40Generally, you probably wouldn't put one thing into a function
10:43But once you have four or five two lines of code that repeat themselves even
10:47It's a good idea to start thinking about creating a function. So like when i'm looking at this for me this uh screams out
10:54Um, I should probably create here a function. So let's just do that really really quickly because you know
10:59I think these type of stuff are so important. So if I would call this um, um change
11:05news
11:06status
11:07And we could have your maybe the um the actual value that's being changed
11:13um, so maybe we'll have your the value
11:16and then we could just basically copy this whole thing and because we're
11:21We're not going to have this anymore
11:23Then we probably would want to send in that this also so we might want to do um, you know
11:27Like maybe we'll call it that that and value and I could just wrap it in that that
11:33And next is just grab that value and the value that is changing is basically this item right here
11:39And then all I need to do is just go back up there create a function such as change new status
11:47Um and send to this
11:50And then I could send into it also the actual height itself that i'm playing with
11:54And then I would do that now i'm not going to do it because i'm going to leave that for you
11:58To figure out if it's worth it for you in this case for me
12:01It's i'm adding a lot more code than it's actually helping me
12:04So i'm probably not going to do it
12:05But those are the type of thought processes that you probably want to think about
12:10When you're creating your code and try to think about that all the time try to
12:14Every time you create something think if it's something that you could then wrap up into a rule
12:19Aka wrap it into a function that you could then reuse again in the future. All right
12:24So we're done with talking about the basic. Um css usages with tween light using the css plugin
12:32And that's it. Let's move on to our next topic. See you in the next lesson
