【发布时间】:2015-08-13 21:06:16
【问题描述】:
我有一个用户界面,其中有一个在点击时会放大和缩小的框。它是动画的,当盒子关闭时,应该运行另一个动画。这两个事件都使用相同的函数和某些条件来处理。
错误是,当框打开时,我单击它,然后在缩小动画完成之前再次单击它,被放大但只有在缩小时才应该运行的动画,被触发。
- 正确行为:http://cl.ly/2M0a3T3e2V0g
- 双击错误行为:http://cl.ly/2l0E3x3L0h19
如何确保只有在缩小完成后才能触发缩小后动画?这是代码(它是有角度的,但在这里并不重要......):
animating = false; // I tried to use a flag, but it did not work
scope.toggleContextSlider = function() { // The click function
if (scope.sliderClosed) { // The "avatar" is the "box"
openSlider(scope.animtionDuration, scope.hearingContextAvatar); // The "avatar" is the "box"
} else {
closeSlider(scope.animtionDuration, scope.hearingContextAvatar);
}
scope.sliderClosed = !scope.sliderClosed;
}
动画功能:
function openSlider(animtionDuration, hearingContextAvatar) {
// Animating stuff
if (hearingContextAvatar && !animating) {
closeStuff(animtionDuration, hearingContextAvatar)
}
}
function closeSlider(animtionDuration, hearingContextAvatar) {
// Animating stuff
if (hearingContextAvatar && !animating) {
openStuff(animtionDuration, hearingContextAvatar)
}
}
function openStuff(animtionDuration, currentAvatar) { // This is the functions that is triggered with the double click
animating = true;
setTimeout(function(){
// Animating stuff
animating = false;
}, animtionDuration);
}
function closeStuff(animtionDuration, currentAvatar) {
animating = true;
currentAvatar.removeClass('open')
setTimeout(function(){
// Animating stuff
animating = false;
}, animtionDuration);
}
【问题讨论】:
-
永远不会调用 openStuff() 和 closeStuff() 函数,因此您的布尔标志永远不会从 false 更改其值。
-
@HankScorpio 他们是,我更新了名字。
-
@ilyo 你在我的解决方案中找到你想要的了吗?
标签: javascript angularjs user-interface animation queue