I am here to continue the discussion around AngularJS. Today, we will discuss about the $animation in AngularJS. Also in case you have not had a look at our previous articles of this series, go through the following links:
In this article, we will discuss about the services that AngularJS provides for animating content changes in the Document Object Model (DOM) and for dealing with touch events.
Animating Elements
The $animate service allows you to provide transition effects when elements are added, removed, or moved in the DOM. The $animate service doesn’t define any animations itself but relies on the CSS3 animation and transition features. Animations can be a useful means of drawing the user’s attention to an important change in the layout of an application, making the transition from one state to another less jarring. Many developers treat animations as an outlet for their frustrated artistic ambition and ladle on as many as possible. The results can be annoying, especially for the user who has to endure endless special effects every time they perform a task. For a line-of-business application, where the user could be repeating the same set of actions all day, the effect is demoralizing beyond description. Animations should be subtle, brief, and quick. The goal is to draw the user’s attention to the fact that something has changed. Use animations consistently, cautiously, and—above all—sparingly.
Installing the ngAnimation Module
The $animation service is defined within an optional module called ngAnimation that must be downloaded into the angularjs folder. Go to http://angularjs.org, click Download, select the version you want and download the file. Download the angular-animate.js file into the angularjs folder.
Defining and Applying an Animation
You don’t work directly with the $animate service to apply animations. Instead, you define animations or transitions with CSS, following a special naming convention, and then apply those names as classes to elements, which also have AngularJS directives.
The Built-in Directives That Support Animation and the Names Associated with Them. The name enter is used when content is shown to the user. The name leave is used when content is hidden from the user. The name move is used when content is moved within the DOM. The names add and remove are used when content is added and removed from the DOM.
Directive | Names |
ng-repeat | enter, leave, move |
ng-view | enter, leave |
ng-include | enter, leave |
ng-switch | enter, leave |
ng-if | enter, leave |
ng-class | add, remove |
ng-show | add, remove |
ng-hide | add, remove |
For demonstrate the animation service in AngularJS, I write down the below program -
app.js
- var testApp = angular.module('TestApp', ["ngAnimate"]);
Index.html
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml" ng-app="TestApp">
- <head>
- <title>Angular Animation</title>
- <script src="angular.js"></script>
- <script src="angular-animate.js"></script>
- <script src="app.js"></script>
- <script src="Index.js"></script>
- <style type="text/css">
- .firstSampleAnimation.ng-hide-add,
- .firstSampleAnimation.ng-hide-remove {
- -webkit-transition: 1s ease-in-out opacity;
- transition: 1s ease-in-out opacity;
- opacity: 1;
- }
-
- .firstSampleAnimation.ng-hide {
- opacity: 0;
- }
- </style>
- </head>
- <body ng-controller="indexController">
- <h1>ngShow animation</h1>
- <button ng-click="fadeAnimation =!fadeAnimation">
- Toggle fade
- </button>
- fadeAnimation value: {{fadeAnimation}}
- <div class="firstSampleAnimation" ng-show="fadeAnimation">
- This element appears when the fadeAnimation model is true
- </div>
- </body>
- </html>
Index.js
- testApp.controller("indexController", function ($scope) {
- $scope.fadeAnimation = false;
- });
After writing the above code, run the index.html file in browser and output will be as below,
Supporting Touch Events
The ngTouch module contains the $swipe service, which is used to improve support for touchscreen devices beyond the basic events. The events in ngTouch module provide notification of swipe gestures and a replacement for the ng-click directive, which addresses a common event problem on touch-enabled devices.
The swipe gestures are useful whenever you want to improve support for touchscreen devices. The ngTouch swipe events can be used to detect left-to-right and right-to-left swipe gestures. To avoid confusing the user, you must ensure that the actions you perform in response to these gestures are consistent with the rest of the underlying platform—or at the very least, the default web browser for that platform. For example, if the right-to-left gesture usually means “go back” in the web browser, then it is important that you do not interpret the gesture in your application in a different way.
The replacement for the ng-click directive is useful for touch-enabled browsers because they synthesize click events for compatibility for JavaScript code that has been written with mouse events in mind. Touch browsers generally wait for 300 milliseconds after the user has tapped the screen to see whether another tap occurs. If there is no second tap, then the browser generates the touch event to represent a tap and a click event to simulate a mouse—but that 300-millisecond delay is just enough of a lag to be noticeable to the user, and it can make an application appear unresponsive. The ng-click replacement in the ngTouch module doesn’t wait for a second tap and issues the click event much faster.
Installing the ngTouch Module
Go to http://angularjs.org, click Download, select the version you want and download the file. Download the angular-touch.js file into the angularjs folder.
Now for demonstrating the ngTouch module, I write down the below code - '
App.js
- var testApp = angular.module('TestApp', ["ngTouch"]);
Touch.html
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml" ng-app="TestApp">
- <head>
- <title>Angular Touch Animation</title>
- <script src="angular.js"></script>
- <script src="angular-touch.js"></script>
- <script src="app1.js"></script>
- <script src="touch.js"></script>
-
- </head>
- <body ng-controller="indexController">
- <div class="panel panel-default">
- <div class="panel-body">
- <div class="well" ng-swipe-right="handleSwipe('left-to-right')" ng-swipe-left="handleSwipe('right-to-left')">
- <h2>Swipe Here</h2>
- </div>
- <div>Swipe was: {{swipeType}}</div>
- </div>
- </div>
- </body>
- </html>
Touch.js
- testApp.controller("indexController", function ($scope, $element) {
- $scope.swipeType = "<None>";
- $scope.handleSwipe = function (direction) {
- $scope.swipeType = direction;
- }
- });
Now, run the touch.html file in browser and output as below,