因此,当用户滚动时,我尝试让几个div滚动到其内容中的某个位置。下面是我当前的代码:
var target = 0;
var main_scroll = 0;
var top_scroll = 0;
var bot_scroll = 0;
var counter = 1;
$('.main-container').on('scroll', function() {
if ($('#trigger-' + (counter + 1)).length && $(this).scrollTop() > target) {
main_scroll = main_scroll + $('.main-container').height();
$('.main-container').animate({
scrollTop: main_scroll
}, 1000);
top_scroll = top_scroll + $('.top-container').height();
$('.top-container').animate({
scrollTop: top_scroll
}, 1000);
bot_scroll = bot_scroll + $('.bottom-container').height();
$('.bottom-container').animate({
scrollTop: bot_scroll
}, 1000);
target = target + $('.main-container').height();
counter++;
} else if ($('#trigger-' + (counter - 1)).length && $(this).scrollTop() < target) {
main_scroll = main_scroll - $('.main-container').height();
$('.main-container').animate({
scrollTop: main_scroll
}, 1000);
top_scroll = top_scroll - $('.top-container').height();
$('.top-container').animate({
scrollTop: top_scroll
}, 1000);
bot_scroll = bot_scroll - $('.bottom-container').height();
$('.bottom-container').animate({
scrollTop: bot_scroll
}, 1000);
target = target - $('.main-container').height();
counter--;
}
});就像现在一样,如果我滚动一次,div就会不断地上下跳动。不是100%确定我做错了什么。
发布于 2016-08-04 23:41:11
实际上,我可以通过使用以下命令来修复此问题:
if ( ! $(this).is(':animated') ) {以确保scroll事件不是由我的动画触发的。
https://stackoverflow.com/questions/38771723
复制相似问题