gpt4 book ai didi

jquery - 到达正文末尾后,向下滚动不会重复

转载 作者:行者123 更新时间:2023-11-27 23:20:59 24 4
gpt4 key购买 nike

我试图从 div 的拳头再次重复滚动,但它只在页面加载时工作一次。

<div class="content">
<div class="next-section blue">First Section</div>
<div class="next-section red">Second Section</div>
<div class="next-section green">Second Section</div>
<div class="next-section yellow">Second Section</div>
</div>
<button class="down-btn">Click to go to the next section</button>
var i = 0;
$('.down-btn').on('click', function(e) {
e.preventDefault();
i++;
var offset = $("div.next-section").eq(i).offset().top;
$('html, body').stop().animate({
scrollTop: offset
}, 400);
});

所以,当滚动结束时,点击这个按钮,应该会从第一部分重新开始链接:https://jsfiddle.net/uj91djeL/1/

最佳答案

你应该在索引变量 i 到达末尾时将其重置为 0,即等于所有部分的长度。

var i = 0;
$('.down-btn').on('click', function(e) {
e.preventDefault();
i++;
var nextSection$ = $("div.next-section")
if(i === nextSection$.length) i=0;
var offset = nextSection$.eq(i).offset().top;
$('html, body').stop().animate({
scrollTop: offset
}, 400);
});

检查 this fiddle

关于jquery - 到达正文末尾后,向下滚动不会重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58000198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com