次のことについて初心者の頭を理解できないので、少しアドバイスが必要です。このフィドルを参照してください:http://jsfiddle.net/NtUpw/
コードは意図したとおりに機能しますが、現在の div オフセットが 41 を超えて prev がヒットした場合、ページを現在の div の先頭ではなく、その前の div の先頭に戻してください。この条件を追加するにはどうすればよいですか?
私は、現在のコードが最もクリーンではないことを認識しています (実際には、2 つのフィドルの組み合わせです)。ありがとう。
$('a.buttons').on('click', function (e) {
e.preventDefault();
var t = $(this).text(),
that = $(this);
if (t === 'next' && $('.current').next('div.post').length > 0 ) {
var $next = $('.current').next('.post');
var top = $next.offset().top;
$('body').animate({
scrollTop: $('.current').next('div.post').offset().top - 40
});
} else if (t === 'prev' && $('.current').prev('div.post').length > 0 ) {
var $prev = $('.current').prev('.post');
var top = $prev.offset().top;
$('body').animate({
scrollTop: $('.current').prev('div.post').offset().top - 40
});
}
$(window).bind('scroll', function () {
$('.post').each(function () {
var post = $(this);
var position = post.position().top - $(window).scrollTop();
if (position <= 40) {
post.addClass('current');
post.prev().removeClass("current");
} else {
post.removeClass('current');
}
});
});