ngIf 和 ngAnimate - 动画期间重复 HTML 内容
ngIf with ngAnimate - duplicated HTML content during the animation
我尝试制作简单的 angular 动画。但是我有一个问题,当我快速点击按钮时(在动画完成之前),内容容器在我点击时创建了很多次...
如何防止这种情况发生?我想在单击后停止动画,然后在同一对象而不是新对象上开始动画?
HTML
<div ng-app="test">
<div class="container" ng-controller="TestController as tCtrl">
<div class="row">
<div class="col-xs-12">
<a class="btn btn-default" ng-click="tCtrl.tt = 1"> 1 </a>
<a class="btn btn-default" ng-click="tCtrl.tt = 2"> 2 </a>
</div>
</div>
<div class="row">
<div class="col-xs-12 animate-if" ng-if="tCtrl.tt === 1">Chosen = 1</div>
<div class="col-xs-12 animate-if" ng-if="tCtrl.tt === 2">Chosen = 2</div>
</div>
</div>
</div>
JS
var app = angular.module('test', ['ngAnimate']);
app.controller('TestController', function ($scope) {
this.tt = 1;
});
CSS
.animate-if.ng-enter, .animate-if.ng-leave {
-webkit-transition: opacity 5.2s ease-in-out;
-moz-transition: opacity 5.2s ease-in-out;
-ms-transition: opacity 5.2s ease-in-out;
transition: opacity 5.2s ease-in-out;
}
.animate-if.ng-enter,
.animate-if.ng-leave.ng-leave-active {
z-index: 0;
opacity: 0;
}
.animate-if.ng-leave,
.animate-if.ng-enter.ng-enter-active {
z-index: 1;
opacity: 1;
}
编辑:
我确实更改了代码以使用 ngRoutes
和 ngView
而不是 ngIf
但 ngAnimate
的行为是相同的。
我认为这可能是 Angular 中的错误,因为在控制台中,我在第 1348 行的 angular-animate.js 中有错误 undefined in not a function
,其中包含 operation.cancel()
并且是一部分的:
forEach(animationsToCancel, function(operation) {
operation.cancel();
});
完整的问题你可以看到here当你点击左边的菜单'Dashboard'和'Users' fast.
编辑 2:
我现在使用的解决方案:
this.fn_change_view = function (str_view, $event) {
if (!this.contentStopAnim) {
this.contentStopAnim = true;
this.active_view = str_view;
$timeout(function () {
template.contentStopAnim = false;
}, 360);
} else {
$event.preventDefault();
}
};
我正在阻止在动画完成之前更改位置 href,但这不是我很高兴的解决方案...
您可以使用 ng-show
和关键帧来制作动画。这是 plunker.
分配 css 个事件。
.ng-hide-add {
animation:0.1s keyFrameLeave ease;
}
.ng-hide-remove {
animation:0.5s keyFrameEnter ease;
}
注册关键帧
@keyframes keyFrameEnter {
0% {
transform: translate(0,100px);
opacity: 0;
}
100% {
transform: translate(0,0);
opacity: 1;
}
}
@keyframes keyFrameLeave {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
您的 html 将如下所示:
<button ng-click="front = !front">click to animate</button>
<div ng-show="front">Displayed</div>
你的js
$scope.front = false;
我尝试制作简单的 angular 动画。但是我有一个问题,当我快速点击按钮时(在动画完成之前),内容容器在我点击时创建了很多次...
如何防止这种情况发生?我想在单击后停止动画,然后在同一对象而不是新对象上开始动画?
HTML
<div ng-app="test">
<div class="container" ng-controller="TestController as tCtrl">
<div class="row">
<div class="col-xs-12">
<a class="btn btn-default" ng-click="tCtrl.tt = 1"> 1 </a>
<a class="btn btn-default" ng-click="tCtrl.tt = 2"> 2 </a>
</div>
</div>
<div class="row">
<div class="col-xs-12 animate-if" ng-if="tCtrl.tt === 1">Chosen = 1</div>
<div class="col-xs-12 animate-if" ng-if="tCtrl.tt === 2">Chosen = 2</div>
</div>
</div>
</div>
JS
var app = angular.module('test', ['ngAnimate']);
app.controller('TestController', function ($scope) {
this.tt = 1;
});
CSS
.animate-if.ng-enter, .animate-if.ng-leave {
-webkit-transition: opacity 5.2s ease-in-out;
-moz-transition: opacity 5.2s ease-in-out;
-ms-transition: opacity 5.2s ease-in-out;
transition: opacity 5.2s ease-in-out;
}
.animate-if.ng-enter,
.animate-if.ng-leave.ng-leave-active {
z-index: 0;
opacity: 0;
}
.animate-if.ng-leave,
.animate-if.ng-enter.ng-enter-active {
z-index: 1;
opacity: 1;
}
编辑:
我确实更改了代码以使用 ngRoutes
和 ngView
而不是 ngIf
但 ngAnimate
的行为是相同的。
我认为这可能是 Angular 中的错误,因为在控制台中,我在第 1348 行的 angular-animate.js 中有错误 undefined in not a function
,其中包含 operation.cancel()
并且是一部分的:
forEach(animationsToCancel, function(operation) {
operation.cancel();
});
完整的问题你可以看到here当你点击左边的菜单'Dashboard'和'Users' fast.
编辑 2:
我现在使用的解决方案:
this.fn_change_view = function (str_view, $event) {
if (!this.contentStopAnim) {
this.contentStopAnim = true;
this.active_view = str_view;
$timeout(function () {
template.contentStopAnim = false;
}, 360);
} else {
$event.preventDefault();
}
};
我正在阻止在动画完成之前更改位置 href,但这不是我很高兴的解决方案...
您可以使用 ng-show
和关键帧来制作动画。这是 plunker.
分配 css 个事件。
.ng-hide-add {
animation:0.1s keyFrameLeave ease;
}
.ng-hide-remove {
animation:0.5s keyFrameEnter ease;
}
注册关键帧
@keyframes keyFrameEnter {
0% {
transform: translate(0,100px);
opacity: 0;
}
100% {
transform: translate(0,0);
opacity: 1;
}
}
@keyframes keyFrameLeave {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
您的 html 将如下所示:
<button ng-click="front = !front">click to animate</button>
<div ng-show="front">Displayed</div>
你的js
$scope.front = false;