详细信息
我有一个内有动画的函数,当单击按钮时,它将.append一个div到body。然后div会使它的位置向下移动一点。当另一个被添加时,所有的div也会向下移动,这样它们就不会重叠。
现在我们遇到问题了!当垃圾邮件按钮创建div时,它们将重叠。
我们怎样才能阻止这一切的发生?
备注:
我试过的
在我看来,我想要创建一个队列,用于您单击该按钮多少次,并在x的时间已经过去(足够完成所有动画)之后按行释放下一个按钮。问题是我无法在jQuery中找到这样的方法。
.queue中使用jQuery,但是找不到一种方法可以帮助解决这个问题。setTimeout,但是这会导致单击延迟。我觉得这是一件很简单的事情,但经过几个小时的搅乱,我自己找不到解决办法。
演示
我已经为这个问题创建了一个演示,我的真实版本是一个插件,因此它有自己的功能。这只是一个非常基本的版本。
$("button").click(function() {
$("body").append("<div></div>");
$($("div").get().reverse()).each(function(index) {
$(this).animate({
top: "+=120px"
}, {
duration: 600,
queue: false
});
if (index >= 2) {
// Fade out and then remove the item
$(this).fadeOut("fast", function() {
$(this).remove();
});
}
});
});div {
width: 400px;
height: 50px;
background: rgba(0, 0, 0, .4);
position: absolute;
top: 0;
right: 0;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click</button>
发布于 2015-09-04 13:34:01
试试这个解决方案..。防止动画,直到动画回调
animated = false;
$("button").click(function() {
if(animated)
return false;
else
animated = true;
$("body").append("<div></div>");
$($("div").get().reverse()).each(function(index) {
$(this).animate({
top: "+=120px",
duration: 600,
queue: false
},function(){ animated = false; });
if (index >= 2) {
// Fade out and then remove the item
$(this).fadeOut("fast", function() {
$(this).remove();
});
}
});
});div {
width: 400px;
height: 50px;
background: rgba(0, 0, 0, .4);
position: absolute;
top: 0;
right: 0;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click</button>
这可能是管理动画队列的起点。
看看这个小提琴
发布于 2015-09-04 14:09:58
向队列中添加动画:
var freeToAnimate = true;
var animationQueue = [];
var i = 0;
$("button").click(function () {
var queueLength = animationQueue.length;
animationQueue.push(i);
i++;
if(queueLength == 0){
animateDivs(i-1);
} else {
return false;
}
});
function animateDivs(animationIndex) {
if(freeToAnimate == true) {
freeToAnimate = false;
var divsAmount = $("div").length;
$("body").append("<div></div>");
$($("div").get().reverse()).each(function(index, el) {
if(index >= 2) {
// Fade out and then remove the item
$(this).fadeOut( "fast", function() {
$(this).remove();
});
}
$(this).animate({
top: "+=120px"
}, 1200, function() {
var indx = animationQueue.indexOf(animationIndex);
animationQueue.splice(indx, 1);
if(index == divsAmount-1 || divsAmount == 0) {
freeToAnimate = true;
if(animationQueue.length != 0) {
animateDivs(animationIndex+1);
}
}
});
});
}
} https://stackoverflow.com/questions/32399213
复制相似问题