• We just launched and are currently in beta. Join us as we build and grow the community.

AngularJS Show/Hide Animation using ngAnimate

Unknown222

Autonomous Tech Innovator
U Rep
0
0
0
Rep
0
U Vouches
0
0
0
Vouches
0
Posts
65
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Step 1 : Add Dependency

In the header portion of your HTML, add the following:

  1. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  2. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  3. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.min.js"

    ></

    script

    >

Take note that your angular-animate version should be the same as your angular version.

Step 2 : Add Style

Include the following style.

  1. .sample-animation

    {
  2. transition

    :

    all linear 0.5s

    ;
  3. }
  4. .sample-animation

    .ng-hide

    {
  5. opacity

    :

    0

    ;
  6. }
  7. #alertDiv

    {
  8. margin-top

    :

    10px

    ;
  9. }

Step 3 : Creating the Body

This contains our sample div to hide/show to show our animation.

  1. <div

    class

    =

    "container"

    >
  2. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Show/Hide Animation using ngAnimate</

    h1

    >
  3. <div

    class

    =

    "text-center"

    >
  4. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary btn-lg"

    ng-click=

    "show()"

    >

    {{ action }}</

    button

    >
  5. </

    div

    >
  6. <div

    class

    =

    "row"

    id

    =

    "alertDiv"

    >
  7. <div

    class

    =

    "col-sm-6 col-sm-offset-3"

    >
  8. <div

    class

    =

    "alert alert-success text-center sample-animation"

    ng-show=

    "myAlert"

    >
  9. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "hide()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  10. This is a sample alert
  11. </

    div

    >
  12. </

    div

    >
  13. </

    div

    >
  14. </

    div

    >

Step 4 : Add the Angular JS script

Lastly, this is our angular js script.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ngAnimate'

    ]

    )

    ;

  2. app.controller

    (

    'myCtrl'

    ,

    function

    (

    $scope)

    {
  3. $scope.myAlert

    =

    false

    ;
  4. $scope.action

    =

    'Show'

    ;

  5. $scope.show

    =

    function

    (

    )

    {
  6. if

    (

    $scope.myAlert

    ==

    false

    )

    {
  7. $scope.myAlert

    =

    true

    ;
  8. $scope.action

    =

    'Hide'

    ;
  9. }
  10. else

    {
  11. $scope.hide

    (

    )

    ;
  12. }

  13. }

  14. $scope.hide

    =

    function

    (

    )

    {
  15. $scope.myAlert

    =

    false

    ;
  16. $scope.action

    =

    'Show'

    ;
  17. }
  18. }

    )

    ;

Don't forget to include the dependency of ngAnimate in your app.

  1. <!DOCTYPE html>
  2. <html

    ng-app=

    "app"

    >
  3. <head

    >
  4. <title

    >

    AngularJS Show/Hide Animation using ngAnimate</

    title

    >
  5. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  6. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.min.js"

    ></

    script

    >
  8. <style

    type

    =

    "text/css"

    >
  9. .sample-animation {
  10. transition: all linear 0.5s;
  11. }
  12. .sample-animation.ng-hide {
  13. opacity: 0;
  14. }
  15. #alertDiv{
  16. margin-top: 10px;
  17. }
  18. </

    style

    >
  19. </

    head

    >
  20. <body

    ng-controller=

    "myCtrl"

    >
  21. <div

    class

    =

    "container"

    >
  22. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Show/Hide Animation using ngAnimate</

    h1

    >
  23. <div

    class

    =

    "text-center"

    >
  24. <button

    type

    =

    "button"

    class

    =

    "btn btn-primary btn-lg"

    ng-click=

    "show()"

    >

    {{ action }}</

    button

    >
  25. </

    div

    >
  26. <div

    class

    =

    "row"

    id

    =

    "alertDiv"

    >
  27. <div

    class

    =

    "col-sm-6 col-sm-offset-3"

    >
  28. <div

    class

    =

    "alert alert-success text-center sample-animation"

    ng-show=

    "myAlert"

    >
  29. <button

    type

    =

    "button"

    class

    =

    "close"

    ng-click=

    "hide()"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  30. This is a sample alert
  31. </

    div

    >
  32. </

    div

    >
  33. </

    div

    >
  34. </

    div

    >
  35. <script

    >
  36. var app = angular.module('app', ['ngAnimate']);

  37. app.controller('myCtrl', function($scope){
  38. $scope.myAlert = false;
  39. $scope.action = 'Show';

  40. $scope.show = function(){
  41. if($scope.myAlert == false){
  42. $scope.myAlert = true;
  43. $scope.action = 'Hide';
  44. }
  45. else{
  46. $scope.hide();
  47. }

  48. }

  49. $scope.hide = function(){
  50. $scope.myAlert = false;
  51. $scope.action = 'Show';
  52. }
  53. });
  54. </

    script

    >
  55. </

    body

    >
  56. </

    html

    >

That ends this tutorial. Happy Coding :)

 

452,292

323,526

323,535

Top