-1

I'm trying to animate a div on click. When i click on a link a div should be appear and that div i clicked disappear. I made an example here:

http://plnkr.co/edit/703244UJ0hcG9GjkOjOz?p=preview

By the way this is some code:

 <a class="" ng-click="showDetails" ng-if="!showDetails">
        Expand 
    </a>


  <div class="animate-if" ng-if="showDetails">

  Details!!
  <a style="cursor:pointer;" ng-click="!showDetails">
      Close
  </a>
  </div>

  {{showDetails}}
</body>

Angular

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

app.controller('MainCtrl', function($scope, $window) {
 showDetails = false;

});

css:

.animate-if.ng-enter, .animate-if.ng-leave {
  -webkit-transition: 1s linear all;
  -moz-transition: 1s linear all;
  -ms-transition: 1s linear all;
  -o-transition: 1s linear all;
  transition: 1s linear all;
}
.animate-if.ng-enter {
  max-height: 0;
  opacity: 0;
}
.animate-if.ng-enter.ng-enter-active {
  max-height: 999px;
  opacity:1;
}
.animate-if.ng-leave {
  max-height: 999px;
  opacity:1;
}
.animate-if.ng-leave.ng-leave-active {
  max-height: 0;
  opacity:1;
}

So, when i click "Expand" should be appear the div with class: animate-if and disappear this one:

<a class="" ng-click="showDetails" ng-if="!showDetails">
        Expand 
    </a>

Then, when i click Close should be disappear the div : animate-if and re-appear the link "Expand" again. Of course using the animation. Actually not working. Not appears or disappears.

2
  • Didn't you forget about including ng-animate.js ? It may be a dumb question but I sometimes forgot it .. Commented May 11, 2015 at 13:29
  • Nope, the ng-animate.js is already included. I'm sure Commented May 11, 2015 at 13:30

1 Answer 1

0

You need a toggle function that will toggle the value of showDetail variable on click expand div.

js :

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

app.controller('MainCtrl', function($scope, $window) {
 $scope.showDetails = false;
 $scope.toggleDetails =function (){
   $scope.showDetails = !$scope.showDetails; 

 }
});

html :

<body ng-controller="MainCtrl">
   <a class="" ng-if="!showDetails" ng-click="toggleDetails()">
        Expand 
    </a>
  <div class="animate-if" ng-if="showDetails">
    Details!!
  <a style="cursor:pointer;"  ng-click="toggleDetails()">
      Close
  </a>
  </div>
   {{showDetails}}
</body>

demo

Sign up to request clarification or add additional context in comments.

7 Comments

Ok it works now with the function but see here the problem; i added ng-if="!showDetails" in the Expand link. So when i click it disappear and appear the div in correct way.. But when i click over "Close" the "Expand" word appears before the div with "Close" disappear.. This is what i mean: plnkr.co/edit/wCWoLxYrId38G2PDLPu1?p=preview
@End.Game i was mistakenly removed the ng-if directive from expand div. happy for you.. :)
Is there a way maybe to detect when the animation finish?
Yes i tried to check that link you posted but without success.. Have you idea how can i use it in my situation?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.