如何正确插入回调函数,使一个函数结束,然后执行另一个函数
How to insert a callback function correctly so as to let one function end and then execute another
我有这个简单的滑块JScript,根据您单击的按钮将缩略图悬停在左侧或右侧。
<script>
$(document).ready(function()
{
$(".right-arrow").click(function(){
$(".box1").animate({right:"1100px"},800);
$(".box2").animate({right:"1100px"},1000);
$(".box3").animate({right:"1100px"},1200);
$(".box4").animate({right:"1100px"},1400);
$(".box5").animate({right:"1100px"},1600);
$(".box6").animate({right:"1100px"},1800);
$(".box7").animate({right:"1100px"},2000);
$(".box8").animate({right:"1100px"},2200);
$(".box9").animate({right:"1100px"},2400);
$(".box10").animate({right:"1100px"},2600);
$(".box11").animate({right:"1100px"},2800);
$(".box12").animate({right:"1100px"},3000);
});
$(".left-arrow").click(function(){
$(".box12").animate({right:"0px"},800);
$(".box11").animate({right:"0px"},1000);
$(".box10").animate({right:"0px"},1200);
$(".box9").animate({right:"0px"},1400);
$(".box8").animate({right:"0px"},1600);
$(".box7").animate({right:"0px"},1800);
$(".box6").animate({right:"0px"},2000);
$(".box5").animate({right:"0px"},2200);
$(".box4").animate({right:"0px"},2400);
$(".box3").animate({right:"0px"},2600);
$(".box2").animate({right:"0px"},2800);
$(".box1").animate({right:"0px"},3000);
});
});
</script>
然而,如果我点击右键,然后快速点击左键,我得到一个拥挤的缩略图"碰撞"与另一个。
我如何插入一个很好的回调函数,以便等待一侧的动画完成,然后以另一种方式进行?
首先,当您在代码中看到模式时,这是一个很好的指示,您应该开始重构:
$(document).ready(function() {
$(".right-arrow").click(function(){
for (i = 1; i < 13; i++){
$('.box' + i).stop().animate({
right: 1000
}, {
duration: 600 + (i * 200),
queue: true
});
}
});
$(".left-arrow").click(function(){
for (i = 1; i < 13; i++){
$('.box' + i).stop().animate({
right: 0
}, {
duration: 3200 - (i * 200),
queue: true
});
}
});
}
关于时间冲突,您所需要的只是传递true
作为队列选项,这将确保后续动画在前一个动画完成后开始。
看看这里的JQuery Animate
var domArray = [$(".box1"), $(".box2"), $(".box3")....];
var runningIndex = 0;
function runAnimate(){
domArray[runningIndex].animate({right:"0px"},800 + runningIndex*200, function(){
if(domArray.length > runningIndex){
runningIndex ++;
runAnimate();
}
});
}
$(".right-arrow").click(function(){
runningIndex = 0;
runAnimate();
});
相关文章:
- 函数1结束后,JavaScript调用函数2
- JavaScript - 回调 - 等待多个函数结束
- 使用 JavaScript 显示 XML 数据,在函数结束时出现问题
- 等待函数结束(在 async.series 中)无法按预期工作
- 函数结束时如何运行代码
- Javascript等待函数结束,包括来自node.js的异步MYSQL查询
- 等待异步 Javascript 函数结束检索结果(延迟?)
- Jquery 执行函数 第三方函数结束后
- 如何在调用它的函数结束时执行回调
- 在所有cordova.exec函数结束后执行成功回调
- 在上一个js函数结束后运行js函数
- 直到函数结束,才会显示模式引导
- 函数结束后显示MessageBox
- Javascript对象在创建它的函数结束后被设置为未定义
- Node.js回调,等待函数结束
- 在函数结束时用javascript播放音乐
- Javascript在另一个异步函数结束时调用函数
- 事件.PreventDefault直到函数结束才会起作用
- 在JavaScript中,等待一个函数结束后再开始另一个函数
- 如何正确插入回调函数,使一个函数结束,然后执行另一个函数