将更多项目添加到菜单时,将内容 div 高度扩展到左侧菜单高度

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

我有一个左侧菜单,我可以在其中添加或删除项目。右边有一个内容 div,它有一个最小高度。

使用 angular 将更多项目添加到左侧菜单时,如果左侧菜单高于其最小高度,我希望内容 div 将其高度扩展到左侧菜单.

还有一点就是我也想要ng-animate的内容下拉效果div.

这里是 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>

使用jQuery:

$("#right").css({'height': $("#left").outerHeight()});

它会将#right 的高度设置为#left 的外部高度。

所以我会回答我的问题。我设法使用 ng-style 指令做到了:

<div id="right" ng-style="{height: hgt+ 'px'}">

然后在控制器中:

$scope.hgt = $('#left').outerHeight() + 40;

http://jsfiddle.net/babvqx8e/22/

无需使用 jQuery 或 ng-style 进行骇人听闻的 DOM 操作。只需将 display: flex 添加到包装器中即可:

#wrapper {
    display: flex;
}

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

使用 flexbox 还可以移除浮动:

#left {
    width: 25%;
    background-color: lightblue;
}

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

查看此 really nice guide to Flexbox 了解更多信息。