所以这里有一个更新的fiddle,它应该满足您正在寻找的要求。有一些变化,但它似乎正在工作。它不使用引导程序,但您的动画需要 ng-animate。
我已经在您的表单组元素周围放置了一个包装器,并将 ng-show 更改为 ng-if。
//html
<button ng-click="showDiv(1)" class="btn">collapse 1</button>
<button ng-click="showDiv(2)" class="btn">Collapse 2</button>
<div class="sec-wrp" ng-if="show === 1">
<div class="form-group" >
<div class="sec">show 1</div>
</div>
</div>
<div class="sec-wrp" ng-if="show === 2">
<div class="form-group" >
<div class="sec">show 2</div>
</div>
</div>
<div class="form-group" ng-if="show==3" >
</div>
click 事件现在使用您要显示的 div 的值调用控制器中的函数。控制器通过将 $scope.show 值设置为 0 来响应。如果 currentShow 值为零(因为它已初始化),它将$scope.show 值设置为传递的值并将currentShow 变量重置为新值没有超时。如果传递的值等于currentShow 值,它会将其视为切换,不重置$scope.show 并将currentShow 重置为零,关闭div。如果传递的 int 不等于 currentShow,则将 currentShow 和 $scope.show 设置为新值。
//controller
var app = angular.module('app', ['ngAnimate']);
app.controller('Test', ['$timeout','$scope', function($timeout, $scope) {
$scope.show = 0;
var currentShow = 0;
$scope.showDiv = function(int){
$scope.show = 0;
if(currentShow === 0){
currentShow = int;
$scope.show = int;
return false;
}
if(currentShow === int){
$timeout(function(){
currentShow = 0;
return false;
}, 1000)
}else{
$timeout(function(){
currentShow = int;
$scope.show = int;
}, 1000)
}
}
}]);
一秒的$timeout 值对应于动画的过渡时间,即允许一秒的等待时间等待 1 秒的动画在你的 CSS 中完成,它使用 ng-enter 和 ng-leave 代替的关键帧。如果您想在此处调整转换时间,请确保您还调整了 $timeouts。
//css
.custom{position: relative; z-index: 2; border: 1px solid #ccc;}
.sec{
padding: 20px;
border: 1px solid orangered;
background: orange;
height: 100px;
z-index: 1;
}
.sec-wrp{
overflow: hidden;
transition: all ease 1s;
}
.sec-wrp.ng-enter{
opacity: 0;
height: 0;
}
.sec-wrp.ng-enter.ng-enter-active{
opacity: 1;
height: 100px;
}
.sec-wrp.ng-leave{
opacity: 1;
height: 100px;
}
.sec-wrp.ng-leave.ng-leave-active{
opacity: 0;
height: 0;
}
希望这会有所帮助。