我希望固定的导航栏上有“下一个”和“上一个”按钮,可以滚动页面到下一个class为"section"的div。
我已经设置了jQuery以添加单击功能到NEXT和PREV hrefs。这个单击函数将使用ScrollTop来移动到下一个class为.section的div。
以下是jQuery:
我已经设置了jQuery以添加单击功能到NEXT和PREV hrefs。这个单击函数将使用ScrollTop来移动到下一个class为.section的div。
以下是jQuery:
$('div.section').first();
// binds a click event-handler to a elements whose class='display'
$('a.display').on('click', function(e) {
// prevents the default action of the link
e.preventDefault();
// assigns the text of the clicked-link to a variable for comparison purposes
var t = $(this).text(),
that = $(this);
console.log(that.next())
// checks if it was the 'next' link, and ensures there's a div to show after the currently-shown one
if (t === 'next' && that.next('div.section').length > 0) {
//Scroll Function
$('html, body').animate({scrollTop:that.next('div.section').scrollTop()});
}
// exactly the same as above, but checking that it's the 'prev' link
else if (t === 'prev' && that.prev('div.section').length > 0) {
//Scroll Function
$('html, body').animate({scrollTop:that.prev('div.section').scrollTop()});
}
});
我目前正在使用有大量注释的jQuery在JSfiddle上工作,以帮助您理解:http://jsfiddle.net/ADsKH/1/
我目前使用console.log检查(that.next())以确定下一个.section是什么,但它给我返回了一些非常奇怪的结果。
为什么这不像预期的那样工作?
.prevButton
和.nextButton
)触发下一个/上一个按钮,这样我们就可以将其替换为图像而不是文本? - Chun