Search code examples
htmlcssangularjsng-animate

Expand content div height to left-menu height when adding more item to menu


I have a left-menu in which I can add or remove item to. On the right there is a content div that has a min-height.

When adding more item into the left-menu using angular, I want the content div expand it height to the left-menu if the left-menu is taller than it's min-height.

One more thing is I also want the pull-down effect of ng-animate for the content div.

Here is the fiddle link: link

var app = angular.module('myApp', ['ngAnimate']);
app.controller('myCtrl', function($scope) {
  $scope.items = ['foo', 'bar']; 
  var num = 0;    
  $scope.add = function() {
    $scope.items.push(num);
    num++;
  }
});
#left {
  width: 25%;
  background-color: lightblue;
  float: left;
}

#right {
  width: 75%;
  float: left;
  background-color: lightgreen;
  min-height: 200px
}

.item {
  height: 20px;
  opacity: 1;
  transition: 0.5s;
}

.item.ng-enter {
  opacity: 0;
  height: 0;    
}

.item.ng-enter-active {
  height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.7/angular-animate.min.js"></script>

<div id="wrapper" ng-app="myApp" ng-controller="myCtrl">
  <div id="left">
    <p class="item" ng-repeat="item in items">
      {{item}}
    </p>
  </div>
  <div id="right">
    <button ng-click="add()">add</button>
    {{names}}
  </div>
</div>


Solution

  • No need for hacky DOM manipulation using jQuery or ng-style. Just add display: flex to the wrapper:

    #wrapper {
        display: flex;
    }
    

    https://jsfiddle.net/babvqx8e/26/

    Using flexbox also allows you to remove the floats:

    #left {
        width: 25%;
        background-color: lightblue;
    }
    
    #right {
        width: 75%;
        background-color: lightgreen;
        min-height: 200px
    }
    

    See this really nice guide to Flexbox for more info.