在ready函数中,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()函数呢?因为我已经实现了complete回调。
$(document).ready(function(){
scrollDown();
});
function scrollDown(){
$('html, body').animate({
scrollTop:$("#4").offset().top-100
},
{
duration:2000,
complete: function(){
scrollUp();
}
});
}
function scrollUp(){
$('html, body').stop();
$('html, body').animate({
scrollTop:$("#1").offset().top-100
},
{
duration:2000,
complete: function(){
scrollDown();
}
});
}