Creating simple jQuery animations
Video: Creating simple jQuery animationsOut of the box jQuery comes with several simple animation types that are immediately useful for very basic use cases. Lets take a look at those now. So here in my sample page I have a few pictures of these flowers. Let's take a look at the code. Alright, so what I'm doing is making a selection of an ID that contains all of these. And now I'm going to operate on the very first link which surrounds this image and when I click I'm going to take some action. First I'm going to make sure that we don't follow the link and then I'm selecting the next one after that because I want to be able to click somewhere and see an effect somewhere else.
Viewers: in countries Watching now:
- What is jQuery?
- Installing jQuery
- Performing multiple operations with chaining
- Using classes
- Adding, modifying, and removing content dynamically
- Triggering a change with event binding
- Creating a dynamic contact form
- Building a slideshow gallery
- Creating simple jQuery animations
Creating simple jQuery animations
Out of the box jQuery comes with several simple animation types that are immediately useful for very basic use cases. Lets take a look at those now. So here in my sample page I have a few pictures of these flowers. Let's take a look at the code. Alright, so what I'm doing is making a selection of an ID that contains all of these. And now I'm going to operate on the very first link which surrounds this image and when I click I'm going to take some action. First I'm going to make sure that we don't follow the link and then I'm selecting the next one after that because I want to be able to click somewhere and see an effect somewhere else.
So what's happening is I'm selecting that one and then I'm using the hide method here to hide it and this is what happens. Click here and the next one disappears. Now okay, this is not actually animation because it just goes away instantly so let me reload I'm going to change this. jQuery has a hide method and a show method and both of them can take a duration as an argument. So, if I put in 2,000, that's 2,000 milliseconds, so now when I click this, this will take two seconds to do the same thing which is animated. Now as I say, jQuery also has a show method.
So if I wanted to show everything in this area that is currently hidden, I could do it like this. Instead of using next one, I can just dive back into my flowers item. Actually I'll use the variable instead of making this selection again. And then, I can find everything in here using the hidden filter. This will use a couple of different techniques to find everything that jQuery considers hidden. And then I'll show them all in two seconds. Now I'll go back over here and reload. And if I click this first element. Here we go, everything that was previously hidden is now shown. So, now we've seen that there are show and hide methods.
There are others as well. Let's revert this to the next one and we'll try a couple others. So there's also a toggle method. This one will do the same thing. I'm going to take out the duration, so this just happens fast. And if you pass a couple of strings into these same methods, this will work for show, hide, and any of the other methods we're going to look at. If you pass in fast or slow, jQuery will use a built-in duration. So I'm going to use fast which will indicate that I want an animation but I want it to go pretty fast. So using the toggle method, every time I click this, jQuery will keep track of whether the element is shown or hidden for me and then do whatever needs to be done, either showing it or hiding it.
So reload again. There we go, nice and fast, disappears, click it again and it comes back. I could do this all day. So in addition to show, hide, and toggle, we can also fade toggle. And I get a nice animated fade in and fade out. That was too fast. Let's slow it down, using the slow keyword. Reload. There we go. And I can also set the duration manually for this. Then we have the slow fade. Here we go.
Now fade toggle is the combined version of fadein and fadeout. So you can also use those, if they're appropriate. One more I want to show you is slide toggle. We'll make this one maybe not quite so painfully slow. Two seconds for this. Reload, and click, and it slides up, and disappears. And then if I click again, it slides down to reappear. So, there are equivalent methods called slide up and slide down, that you can use. In this case, it may not be obvious, but just to be clear, slide up is the one that makes an element disappear, and then slide down is the one that makes it reappear, and these take all the same arguments.
Fast, slow, no argument at all. Whatever you want. So one more useful thing that I want to mention is that of course you don't have to do everything on click. I have a block of code coming out down here that shows how you can do things on mouse-over. In this case, I'm using the mouse enter and mouse leave functions. So let me save this, we'll see what it does first and then we'll take it apart. So I will reload and now when I mouse over these elements, it's showing a little tool tip that's using a fade effect. So, how does this work? Let's inspect this just so we can see what the elements are.
We're going to expand this window to make it easier. So the tool tip is coming from this DIV which has a class of info, it starts out hidden. And so what I'm doing is when I either mouseenter or mouseleave that element, I first preventDefault, not that there is a default action on hover, but it's there, and then I'm taking this, which is the link and looking for any children of that, that have the class info and I'm using fadeToggle on those. So, on this one it grabs its own child info and fades it in and fades it out and that works on any of these.
And of course you can use any animation method you want. You can come in and out this one and you slide toggle instead. Reload and there we go. So, all these methods are built in jQuery core and you can use them on anything. They're really handy and really easy.
There are currently no FAQs about jQuery for Web Designers.