Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
This course is a short companion piece to jQuery for Web Designers. See that course for information about building your core jQuery skill set.
Skill Level Intermediate
jQuery UI extends the built-in animation capabilities of jQuery Core, pretty extensively. We're going to look at some of the ways that it does that in this movie. So here I have an image and a little box with a colour. The first thing that we'll look at is some built in effects,. The jQuery UI effects library takes some of the existing capabilities and packages them up in ways that are convenient to call in your code. So for example, there's a puff effect. Make the flowers go puff, and they disappear. It's basically just scaling, and fading them out, but it does it all at once, in one named function.
Let's see what that looks like. going to jump down to the bottom of this file, scroll up a little bit, and here it is. I'm triggering this first on the click of this button, and then. I'm just calling the effect method on the selected image. The effect is called puff. I just call it by name with that string. And then I give it a duration, which is 300 milliseconds, in this case. So, it's a nice fast puff and it disappears. Likewise there's a bounce effect. Which does just what you might expect. I have that triggered on that second button. And here it is. The call is almost identical.
I've made it a little longer, and I'm just calling it by its name, bounce. Now, many of these named effects have options that you can pass in. So let's look at the puff effect with a bunch of options set. So I've uncommented this block. And commented out that one liner. Save this. We'll take at how it works, and then we'll take it apart. So, reload over here. And you can see it's a little slower. And then afterwards, it brings the flowers back. So, how does this work? Well, the beginning of it is the same. I'm just calling puff.
And then, I'm also passing in an easing function. jQuery UI, if you include the Effects Library, includes all of the easings that you see in the jQuery Easings Plugin. So, you don't need to install those side by side. If you include jQuery UI and its effects, you don't need the jQuery Easing Plugin separately. And the way you call them is by passing in an object with the name, easing and then the value which is the name of the easing function your're using. Then the duration and then the final argument that the affect method accepts is a function that is a callback that you can call when the animation completes.
So in this case, I'm just saying show this. This of course refers to the element with the ID image that I'm operating on. And just show it again in 500 milliseconds using the built in jQuery show method. And that's what it looks like. So you can look through the documentation to see what options are available for the different effect functions. Note that the arguments can seem to be in a different order at times. Like the duration here is the second argument, while here it's the third. There is a canonical order that the documentation will show you. jQuery is pretty smart about figuring out what arguments map onto which properties you're trying to set, but it's always a good idea to keep things in the expected order.
So, another thing jQuery UI offers is the ability to animate colour. This is something that's not built into jQuery Core, it is technically a numeric proper, so maybe you think it'd be something that you can animate using the animate function. But you need jQuery UI to do that. So, this is what it looks like. On this box I am animating both the background colour and the foreground colour. Anything that jQuery can animate that can have a colour assigned to it, can have its colour animated using this capability. So let's look at that trigger. That's right here. So, this is a DIV with the ID colour me, and I'm just animating both it's foreground colour and it's background colour, to these values.
Over 3.5 seconds and I'm just using the default easing, although I've called it by name. So if I reload that's what it looks like. Now in addition to just being able to animate the colour, I can also animate class transitions. What I mean by that is, in jQuery Core, I can add classes, toggle them, basically do a lot of class manipulation on any element. jQuery UI extends that so that I can animate the properties that change when the class changes. For example, this box has this color. I'm actually going to need to reload this.
It has this color when we start out. And when I add this changed class, using this second trigger, it changes. Now, it didn't animate in this case. The way this works is jQuery UI extends the existing methods so they can accept a duration. So I'm going to add a half second animation duration. And reload. Now when I click Give the Box Class, same thing happens, only it's animated. And this is toggle class, so it works in both directions. And this works on more than just colour. It works on any property that jQuery can animate.
So, scrolling back up to the triggers that work on the flower. Here we go. I have another class called, small. Let's look at what that does. Here's the image div. And here is the small class that I've applied to it. So what we're doing is, we're taking the width down from 450 to 200. And then increasing the border width to five pixels. So if I just click this to add the class, you can see it does what I asked it to, but there's no animation. Scroll down to the trigger, here we are, and now I'm going to add a duration, so that this change will animate.
And I'll change this to toggle, so we can try it in both directions. Okay, I'll save, come back over here, reload, and there we go. Animating it down and then animating it back up. So this works as a nice substitute for having to put your CSS in animate function here in jQuery Code. You can keep all your styles up with your CSS and then when you change those classes, jQuery will animate things for you. If you're a more advanced user of CSS this might look somewhat similar to CSS transitions where you can change classes or respond to things with CSS changes when events happen.
Indeed it is a lot like that. If I drop in these transition properties. And take out the duration on this toggle class so that we're just doing a straight toggle class with no animation from jQuery, and reload. You can see the effect is basically the same. Now, using CSS transitions is not quite as compatible as jQuery, but it does open up the possibility of using other properties that jQuery can't animate. Like, for example. Transformations. So if I add this to my small class now and reload.
Now when I add the class it rotates as well. Now this is something that jQuery can't do. It's a CSS transform that's implemented by the browser. It's not a simple numeric property that jQuery can animate. And of course this works bi-directionally. But, though CSS can do this particular kind of transformation, and jQuery cannot, CSS cannot respond to clicks, and jQuery can. So, you can combine the two in nice ways to get all kinds of interseting effects. So, that's a look at jQuery UI Effects and how you can combine them with CSS to give you a lot of power in animating your pages.