目前,我正在尝试使用 jQuery 滚动构建一个站点。我的代码有效,但我的问题是使用我的代码,一旦到达底部并手动向上滚动,向下按钮就会停止工作。即使在手动滚动到顶部之后,如何让向下按钮起作用?
我当前的 html 代码:
<div class="down">next post</div>
<div class="container">
<div class="posts"></div>
<div class="posts"></div>
<div class="posts"></div>
<div class="posts"></div>
<div class="posts"></div>
</div>
和我的 jquery 代码:
var $currentElement = $('.posts').first();
$('.down').click(function() {
var $nextElement = $currentElement.nextAll('.posts');
if($nextElement.length) {
$currentElement = $nextElement;
$('html, body').stop(true).animate({
scrollTop: $nextElement.offset().top
}, 900, 'swing')
}
return false;
});
最佳答案
使用这个 fiddle ...
var currentElement = $('.posts').first();
$('.down').click(function() {
var nextElement = currentElement.nextAll('.posts');
console.log(nextElement);
if(nextElement.length) {
currentElement = nextElement;
$('html, body').stop(true).animate({
scrollTop: nextElement.offset().top
}, 900, 'swing');
}
return false;
});
关于jquery - 滚动到下一个/上一个 div 不适用于手动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48595904/