javascript - 递归调用java script animate函数,每次循环后时间处理
问题描述
在准备好的函数scrollDown动画开始,在scrollDown的完整回调监听器中我调用scrollUp函数开始向上滚动动画。循环完成后,向下滚动动画需要一些时间才能重新开始。更重要的是,每个周期完成后延迟时间都会增加。
$(document).ready(function(){
scrollDown();
});
function scrollDown(){
$('html, body').animate({
scrollTop:$("#4").offset().top-100
},
{
duration:2000,
complete: function(){
scrollUp();
}
});
}
function scrollUp(){
$('html, body').animate({
scrollTop:$("#1").offset().top-100
},
{
duration:2000,
complete: function(){
scrollDown();
}
});
}
解决方案
添加$('html, body').stop(); 成功了,现在动画很流畅,没有延迟时间。但是为什么我必须调用他的 stop() 函数呢?因为我已经实现了完整的回调。
$(document).ready(function(){
scrollDown();
});
function scrollDown(){
$('html, body').stop(); // no idea why I need to call this stop function
$('html, body').animate({
scrollTop:$("#4").offset().top-100
},
{
duration:2000,
complete: function(){
scrollUp();
}
});
}
function scrollUp(){
$('html, body').stop(); // no idea why I need to call this stop function
$('html, body').animate({
scrollTop:$("#1").offset().top-100
},
{
duration:2000,
complete: function(){
scrollDown();
}
});
}
推荐阅读
- sql - 如何使用联合从另一个表中添加新行?
- typescript - 打字稿中的抽象函数返回类型
- sql - 从表中插入数据时,临时表列中的“9.67609e+008”值得到更新
- powershell - Powershell中Windows调度程序中计划任务状态的表格格式
- c# - 应用代码混淆后 exe 需要 1-3 分钟才能运行
- sql - SQL - 如何在特定列中选择具有最大值的数据行?[第2部分]
- python - 将 SSH 与应用程序服务器一起使用时 libcrypto.so 中的段错误
- java - 需要使用java读取对象内部的json数组
- amazon-web-services - 将文件直接从 S3 移动到 FTP
- googletest - “SetUpTestCase”或“TearDownTestCase”的拼写错误问题