我正在尝试为整页滚动编写一些代码,但我陷入了一种递归函数。我想向上或向下滚动整页,具体取决于滚动的方向。我使用滚动动画。问题是,当动画处于运行状态时,会调用scroll事件,它就像是某种无限循环。我停止滚动事件直到动画结束?
$(window).scroll(function(event){
event.preventDefault();
var st = $(this).scrollTop();
if (st > lastScrollTop){
var scroll = "#section2";
current_scroll = "#section2";
alert(current_scroll);
$('html, body').animate({
scrollTop: $(scroll).offset().top
}, 2000);
} else {
var scroll = "#section1";
current_scroll = "#section1";
alert(current_scroll);
$('html, body').animate({
scrollTop: $(scroll).offset().top
}, 2000);
}
lastScrollTop = st;
});