【发布时间】:2015-03-14 02:17:41
【问题描述】:
所以我有this code,它允许我使用 previous 和 next 按钮在 div 之间滚动,脚本运行良好,尽管我试图找到一种方法解决一个小问题,即当我使用鼠标滚轮或滚动条正常滚动然后返回使用按钮滚动时,按钮将页面滚动到我之前使用按钮滚动的位置,我希望这继续我例如,如果我用鼠标滚轮滚动到 div nr.3,我希望它在单击下一个按钮时继续滚动到 div nr.4。也许通过创建一个 :visible 语句或其他东西可以完成这项工作,但我不知道如何编写它。这是代码
$('div.postSection').first();
$('a.display').on('click', function(e) {
e.preventDefault();
var t = $(this).attr('id'),
that = $(this);
if (t === 'nextButton' && $('.currentPost').next('div.postSection').length > 0) {
var $next = $('.currentPost').next('.postSection');
var top = $next.offset().top;
$('.currentPost').removeClass('currentPost');
$(function () {
$next.addClass('currentPost');
$('html, body').animate({scrollTop: $('.currentPost').offset().top-55 }, 'slow');
});
} else if (t === 'prevButton' && $('.currentPost').prev('div.postSection').length > 0) {
var $prev = $('.currentPost').prev('.postSection');
var top = $prev.offset().top;
$('.currentPost').removeClass('currentPost');
$(function () {
$prev.addClass('currentPost');
$('html, body').animate({scrollTop: $('.currentPost').offset().top-55 }, 'slow');
});
}
});
【问题讨论】:
标签: javascript jquery scroll