Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Callbacks: What to do when the animation ends

From: jQuery for Web Designers

Video: Callbacks: What to do when the animation ends

See animation looks pretty good. Everything's going to the right place. But it's still missing something. When the animation completes, these things are still left over. It would be nice to get rid of those as soon as the animation is done. In this movie, we're going to look at call backs which are a generalized way of dealing with events in the middle of other things you're trying to do. So looking back at the code, here is my ghost which I'm animating. So at first I have my object with all the things I want to animate. Then I show the duration.

Callbacks: What to do when the animation ends

See animation looks pretty good. Everything's going to the right place. But it's still missing something. When the animation completes, these things are still left over. It would be nice to get rid of those as soon as the animation is done. In this movie, we're going to look at call backs which are a generalized way of dealing with events in the middle of other things you're trying to do. So looking back at the code, here is my ghost which I'm animating. So at first I have my object with all the things I want to animate. Then I show the duration.

I could plug-in an easing function here if I wanted to. In fact, let's just do that for fun. And then I can add a call back, so what is a call back exactly? A call back is a function that you call at a certain time. Different methods might offer multiple call backs or just one, and they'll fire it different times in response to different parts of the original function. So the original function in this case is animate, and it offers one place for a call back. Which is when the animation completes. So I can put in a name function here, like animation done which I could define somewhere else.

In a case like this where its just one function that I am only going to call in this place. I like to use an anonymous function, so once this function is called something is going to happen, so lets start with something simple, just log a message to the console, and say I've made it. So, this animation will run. It'll animate these properties in this amount of time, using that easing. And when it finishes, it will fire this message. And just to be clear here, you might think that if I put this same thing down here like say console.log ('Here I am') You might think that this would happen as soon as the animation completes.

But let's take a look at how it actually works in practice. I'm going to increase the length of the animation just to make this that much clearer. So I'll switch back to my browser, and open up the Javascript console, and reload. So here we go. You might be expecting one of these to come up in the console before the other. Maybe you'd expect them to appear at the same time. Let's see how it actually works. Here I am, fires immediately, and then when this flower makes it into the cart. And only then do we see the other message. So yes, this fires after the animation starts.

But if I want to do something specifically when the animation completes, I need to use a callback. So one thing that might be nice in this case. I'm going to get rid of the console. Would be to make this disappear as soon as it arrives at its destination so we just animate the fact that its going into the cart. And then when it gets there it goes away, here is how we can do that, we can use this again. So we see this up here which makes reference to this up here, this is a context dependent keyword, so here's one context where this prefers to that selected item and now down here I am using the animate method on ghost, so if I use this down here it refers to ghost.

And what I can do is call remove to remove ghost from the dawn I'll save this and now reload, click this, and there we go, it animates over there and disappears. And this will work on all of these. So, especially with animation where timing is critical. Look into the use of call backs. Animate offers a completion callback, and so do many of the other simple ones that we've looked at. So even when you're using a simple animation You can do things, specifically when the animation completes.

No sooner, no later, through the use of call backs.

Show transcript

This video is part of

Image for jQuery for Web Designers
jQuery for Web Designers

37 video lessons · 12813 viewers

Joe Chellman
Author

 
Expand all | Collapse all
  1. 2m 17s
    1. Welcome
      1m 4s
    2. What you should know
      37s
    3. Using the exercise files
      36s
  2. 12m 0s
    1. jQuery is a JavaScript library?
      1m 15s
    2. When to use jQuery
      2m 51s
    3. Alternatives to jQuery
      1m 25s
    4. Which version of jQuery to use
      1m 50s
    5. How to install jQuery
      3m 21s
    6. Reference materials
      1m 18s
  3. 19m 19s
    1. Getting ready
      2m 26s
    2. Selecting elements to use
      3m 54s
    3. Performing multiple operations on the same line with chaining
      2m 30s
    4. Using classes to find what you're looking for
      3m 52s
    5. Adding, modifying, and removing content dynamically
      4m 3s
    6. Challenge: Form feedback
      1m 12s
    7. Solution: Form feedback
      1m 22s
  4. 18m 28s
    1. Triggering a change based on activity with event binding
      4m 37s
    2. Reading and changing values
      4m 17s
    3. Working with HTML attributes
      4m 55s
    4. Challenge: Dynamic contact form
      1m 27s
    5. Solution: Dynamic contact form
      3m 12s
  5. 16m 42s
    1. Using the Colorbox plugin to build a slideshow gallery
      4m 22s
    2. Implementing Colorbox on your site
      2m 46s
    3. Changing Colorbox options
      5m 53s
    4. Challenge: Convert to a slideshow
      1m 6s
    5. Solution: Convert to a slideshow
      2m 35s
  6. 28m 37s
    1. Using jQuery or CSS to animate elements
      2m 24s
    2. Creating simple jQuery animations
      4m 35s
    3. Animating numeric properties with animate()
      4m 56s
    4. Understanding animation easing
      4m 4s
    5. Putting it together: Flowers in the cart
      6m 4s
    6. Callbacks: What to do when the animation ends
      3m 27s
    7. Challenge: Improve the animation
      1m 3s
    8. Solution: Improve the animation
      2m 4s
  7. 4m 45s
    1. More fun plugins
      2m 38s
    2. What's next: More jQuery
      50s
    3. What's next: More JavaScript
      1m 17s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed jQuery for Web Designers.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.