1

My checkbox input toggles my myDiv using ng-show. I would like this to look fancy. Thus, I'm using a transition effect, using angular-animate.js.

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
<script>
    var app=angular.module('ang_app', ['ngAnimate']);
    app.controller('ang_control01_main', function($scope) {

    });
</script>
<style>
    div {
        transition: .5s;
        height: 100px;
        background-color:lightblue;
    }
    .ng-hide { /* using .ng-show here doesn't work btw */
        height: 0;
    }
</style>
<body ng-app="ang_app" ng-controller="ang_control01_main">
    <input type="checkbox" ng-model="myCheck">
    <div id="myDiv" ng-show="myCheck"></div>
</body>

(http://jsfiddle.net/gfwrknpr/)

Works fine.

However, if I change the selector from div to #myDiv, the animation is gone. Why?

phil294
  • 10,038
  • 8
  • 65
  • 98

1 Answers1

3

change your css to:

#myDiv{
    transition: .5s;
    height: 100px;
    background-color:lightblue;
}
#myDiv.ng-hide { /* using .ng-show here doesn't work btw */
    height: 0;
}

and it will work

Austin
  • 1,291
  • 10
  • 19
  • Is there any explanation why it worked? I'm curious about this. +1 for your answer. – Arun Shinde Apr 01 '16 at 15:34
  • I just went off of the angular animate docs example. I guess when you specify a specific id for the transition, you have to pair that with a specific id for the ng-hide for it to match them up and perform the effect – Austin Apr 01 '16 at 17:36