Chapter 3: Creating removeClass animations with ngClass

by billy roberts

HTML

<script src="https://code.angularjs.org/1.3.2/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.2/angular-animate.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="Ctrl">
        <button ng-click="displayToggle=!displayToggle">
            Toggle Visibility
        </button>
        <div class="container">
            <span class="prompt">Wake up, Neo...</span>
            <div class="cover" 
                 ng-class="{blackout: displayToggle}">
            </div>
        </div>
    </div>
</div>

CSS

.container {
    background-color:black;
    width:200px;
    height:200px;
    overflow:hidden;
}
.prompt {
    position:absolute;
    margin:10px;
    font-family:courier;
    color:lime;
}
.cover {
    position:relative;
    width:200px;
    height:200px;
    left:200px;
    background-color:black;
}
.blackout {
    left:0;
}

JavaScript

// with jQuery

angular.module('myApp', ['ngAnimate'])
.controller('Ctrl', function($scope) {
    $scope.displayToggle = true;
})
.animation('.blackout', function() {
    return {
        removeClass: function(element, className, done){
            if (className==='blackout') {
                $(element)
                .removeClass('blackout')
                .css('left', 0)
                .animate(
                    {'left': '200px'}, 
                    3000, 
                    function() {
                        $(element).css('left','');
                        done();
                    } 
                );
            } else {
                done();
            }
        }
    };
});