AngularJS Show/Hide Animation using ngAnimate
Submitted by nurhodelta_17 on Saturday, January 20, 2018 - 14:32.
Step 1 : Add Dependency
In the header portion of your HTML, add the following:
Take note that your angular-animate version should be the same as your angular version.
Step 2 : Add Style
Include the following style.- .sample-animation {
- transition: all linear 0.5s;
- }
- .sample-animation.ng-hide {
- opacity: 0;
- }
- #alertDiv{
- margin-top: 10px;
- }
Step 3 : Creating the Body
This contains our sample div to hide/show to show our animation.Step 4 : Add the Angular JS script
Lastly, this is our angular js script.- var app = angular.module('app', ['ngAnimate']);
- app.controller('myCtrl', function($scope){
- $scope.myAlert = false;
- $scope.action = 'Show';
- $scope.show = function(){
- if($scope.myAlert == false){
- $scope.myAlert = true;
- $scope.action = 'Hide';
- }
- else{
- $scope.hide();
- }
- }
- $scope.hide = function(){
- $scope.myAlert = false;
- $scope.action = 'Show';
- }
- });
Full HTML
- <!DOCTYPE html>
- <html ng-app="app">
- <head>
- <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
- <style type="text/css">
- .sample-animation {
- transition: all linear 0.5s;
- }
- .sample-animation.ng-hide {
- opacity: 0;
- }
- #alertDiv{
- margin-top: 10px;
- }
- </style>
- </head>
- <body ng-controller="myCtrl">
- <div class="container">
- <div class="text-center">
- </div>
- <div class="row" id="alertDiv">
- <div class="col-sm-6 col-sm-offset-3">
- <div class="alert alert-success text-center sample-animation" ng-show="myAlert">
- This is a sample alert
- </div>
- </div>
- </div>
- </div>
- <script>
- var app = angular.module('app', ['ngAnimate']);
- app.controller('myCtrl', function($scope){
- $scope.myAlert = false;
- $scope.action = 'Show';
- $scope.show = function(){
- if($scope.myAlert == false){
- $scope.myAlert = true;
- $scope.action = 'Hide';
- }
- else{
- $scope.hide();
- }
- }
- $scope.hide = function(){
- $scope.myAlert = false;
- $scope.action = 'Show';
- }
- });
- </script>
- </body>
- </html>
Add new comment
- 195 views