jQuery rola para a próxima classe Div com o botão Next / Previous
O que eu gostaria é de navegação fixa, com os botões NEXT e PREV para, basicamente, rolar a página para o próximo div com a classe de "seção".
Eu configurei o jQuery para adicionar essencialmente uma função de clique aos hrefs NEXT e PREV. Esta função de clique irá então usar o ScrollTop para passar para o próximo duv com uma classe de .section.
Aqui está o 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()});
}
});
Atualmente estou trabalhando em JSfiddle com jQuery fortemente comentado para ajudar você a digerir:http://jsfiddle.net/ADsKH/1/
Eu tenho um console.log atualmente verificando (that.next ()) para determinar o que a próxima seção será, mas está me dando de volta alguns resultados muito estranhos.
Por que isso não está funcionando como pretendido?