【发布时间】:2015-12-29 08:55:38
【问题描述】:
我使用 ng-repeat 堆叠了几个 div,并且我正在使用 ng-leave 以便在删除 div 时,它的高度转换为 0px。
这一切都很好。
但是,当我在删除 div 之前的任何时候修改它的高度时,我遇到了困难。例如,如果我单击 div 上的“Animate Height”按钮,然后尝试删除它,ng-leave 动画将不会运行——它只是在消失之前等待 1 秒。
FIDDLE: https://jsfiddle.net/c1huchuz/32/
HTML:
<body ng-app="myApp" ng-controller="myController">
<button ng-click="add_div()">Add Div</button>
<div id="wrapper">
<div ng-repeat="x in random_array" class="slide">
{{$index}}
<button ng-click="remove_div($index)">Delete</button>
<button ng-click="change_size($index)">Animate Height</button>
</div>
</div>
</body>
CSS:
.slide {
position: relative;
width: 160px;
height: 30px;
background-color: orange;
border: 1px solid black;
}
.slide.ng-enter {
transition: all 1s;
top: -30px;
z-index: -1;
}
.slide.ng-enter.ng-enter-active {
top: 0px;
z-index: -1;
}
.slide.ng-leave {
transition: all 1s;
height: 30px;
z-index: -1;
}
.slide.ng-leave.ng-leave-active {
height: 0px;
z-index: -1;
}
JS:
var app = angular.module("myApp", ['ngAnimate']);
app.controller("myController", ["$scope", function($scope) {
$scope.random_array = []
$scope.add_div = function() {
var x = $scope.random_array.length
$scope.random_array.push(x)
}
$scope.remove_div = function(index) {
$scope.random_array.splice(index, 1)
}
$scope.animate_height = function(index) {
$(".slide:nth-child("+(index+1)+")").animate({"height":"60px"}, 1000).animate({"height":"30px"}, 1000)
}
}]);
【问题讨论】:
标签: javascript jquery angularjs ng-animate