Animating with JavaScript

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 11 years ago
Updated 5 years ago

In this lesson John looks at how to integrate the popular TweenMax library with ngAnimate to get animation effects in an AngularJS application.

John Lindquist: Let's switch from using styles for animations to using JavaScript. I removed the "styles.css" from here and I'm just going to add the TweenMax library.

TweenMax is a popular library for doing animations. It's super fast, very robust, tons of features. Definitely check it out, and I'll probably even do more videos on it because I like it so much.

Add the animation of ".toggle" because it's still a style name, you'll see here,. The class is "toggle" so because it's a class, add the dot.

Let's return an object, since it's ng-if animation that we're doing, the object has a leave. The leave will take a parameter of element. With that element you can do an animation when it triggers the leave.

I'll say TweenMax from-to, we'll say take the element for one second. Take it from an opacity of one to an opacity of zero. Then once we refresh over here, you can see I hit this and it will go away.

One thing I have to point out though, is if you inspect this element here, you can see that it's still there. That means that the ng-if hasn't applied and removed the element. That's because the second parameter here is a callback to "Done", so that once our animation is complete we'll assign that to "Done".

Once our animation is complete, it needs to call "Done". Now you can see if we try and inspect this, it's no longer there. If you can see the "DOM" on this tiny thing, it wouldn't be there either.

Basically it means that it has called "Done", it's removed the element, completely gone. If you forget to call "Done" on some of these animations, you'll just have things lying around, so never forget to call "Done".

Then the second part of this is just doing a very, very similar thing with "Enter". I will just copy this line, so we'll say TweenMax from-to. We'll start with an opacity of zero, because it's going to come in and be added, and then animate up to an opacity of one. We'll need the element and "Done" in here as well.

Once that's complete, we'll hit "Refresh", click "Toggle". It'll fade out. Click "Toggle" again. It'll fade back in.

If you actually watch, let me drag and make this a bit bigger, and will show you this element leaving and coming back in just so it makes a bit more sense.

Select it, and you can see once I click "Toggle"...I can make this a bit bigger too...once I click "Toggle", you saw the value going down and now the element has disappeared. Hit "Toggle" again, and you can see the value is going up. You can see what's going on there if you watch carefully. Leaving...coming back in.

That's the animation in JavaScript. TweenMax is an awesome from-to for these things. Again it's just going from one to zero. I think it makes a lot of sense, and you just use the animation service to define the "toggle" class and make sure that's mapped to the class here. There's many other ways to do animations, I hope to cover most of them...

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today