Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collapse / expand animation in Angular

Tags:

css

angularjs

I have some code like this

<div ng-click="showMore = true">show more</div>
<div class="show-more' ng-show="showMore">
   <div class="cell">
       //some span
   </div>
   <div class="cell">
       //some div
   </div>
   <div ng-click="showMore = false">show less</div>
</div>

and when user clicks 'show more', I want to show the rest in animation

in my css

.cell {
    padding-top: 20px;
    padding-bottom: 15px;
    height: 110px;
}

I have to get rid of ng-show or ng-hide, because display:block or display:none would not make transition: height 0.5s linear; work. So I tried to set the height of .cell to be 0 when it is collapsed, and set it as 110px when it is expanded, but cell height is not really 0 when it is collapsed, because it contains <span> or <div> which have height and padding

how to animate the expand/collapse process in my case? thanks in advance

like image 504
lynn.fang Avatar asked Nov 24 '25 07:11

lynn.fang


1 Answers

var app = angular.module("myApp", []);
 
// Create a Controller named "myCtrl"
app.controller("myCtrl", function($scope) {
  showMore = false;
});
.show-more {
  -webkit-transition: max-height 1s;
  -moz-transition: max-height 1s;
  -ms-transition: max-height 1s;
  -o-transition: max-height 1s;
  transition: max-height 1s;
  background: #e5feff;
  overflow: hidden;
  max-height: 0;
}
.show-more.show {
  max-height: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <div class="show-more" ng-class="{'show': showMore}">
     <div class="cell">
         //some span
     </div>
     <div class="cell">
         //some div
     </div>
  </div>
  <button type="button" ng-click="showMore = !showMore">
    Show {{ showMore ? 'Less' : 'More' }} ...
  </button>
</div>
like image 191
Yordan Nikolov Avatar answered Nov 26 '25 23:11

Yordan Nikolov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!