Animating with JavaScript

AngularJS - Animating with JavaScriptAngularJS Video Tutorial by John Lindquist

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


index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <title>Egghead Videos</title>
    <link rel="stylesheet" href="bower_components/topcoat/css/topcoat-desktop-light.css">
</head>
<body class="well-lg" ng-app="app" ng-controller="AppCtrl as app">

<button class="topcoat-button--large--cta" ng-click="app.toggle=!app.toggle">Goodbye, fair notification</button>
<hr/>
<div class="topcoat-notification toggle" ng-if="app.toggle">I'm too young to fade! :(</div>

<script type="text/javascript" src="bower_components/greensock/src/uncompressed/TweenMax.js"></script>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-animate/angular-animate.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>

app.js

var app = angular.module("app", ["ngAnimate"]);

app.controller("AppCtrl", function() {
    this.toggle = true;
});

app.animation(".toggle", function () {
    return {
        leave: function (element, done) {
            element.text("Nooooo!!!");
            TweenMax.to(element, 1, {opacity:0.5})
        },
        enter: function (element, done) {
            element.text("Yay, I'm alive!")
            TweenMax.from(element, 1, {opacity:0})
        }
    }
})

bower.json

{
  "name": "AnimationTutorials",
  "version": "0.0.0",
  "authors": [
    "johnlindquist"
  ],
  "license": "MIT",
  "private": true,
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "dependencies": {
    "angular-animate": "~1.2.12",
    "angular": "~1.2.12",
    "greensock": "~1.11.4",
    "topcoat": "~0.8.0"
  }
}

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...

Tech logo bar

Don't miss out on the latest PRO lessons.

Get Your PRO Subscription Now

Because you like code... not PowerPoint slides.