2013-02-28 12 views
7

私が望むのは、固定セクションのナビゲーションです.NEXTとPREVボタンは基本的にページを次のdivに「セクション」のクラスでスクロールします。jQuery Next/Previousボタンを使って次のDivクラスにスクロール

基本的にクリック機能をNEXTとPREVのhrefに追加するようにjQueryをセットアップしました。このクリック機能はScrollTopを使用して、次のduvに移動します。ここで

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/

私は現在、(that.next(のためにconsole.logをチェックしています))次のセクションがどうなるかを判断するが、それは私に非常に奇妙な結果を返す。

これが意図したとおりに機能しないのはなぜですか?

答えて

9

thatはがネストされているため、.navigationにありません。

.next()のjQueryドキュメントから。

一致する要素のセット内の各要素の直後の兄弟を取得します。

Here's a working example based on your code

+0

うわー何をシンプルなソリューション!...これは文字通り時間のカップルのための私の脳をラッキングました。 助けてくれてありがとう! – richie

+1

Firefoxで上記のコードがうまくいかない理由は誰にでも分かりますか? 私はすべてのドキュメントをラップしようとしました。準備ができていますが、動作していないようです。 これが機能しなくなるFirefoxには何か固有のものがありますか?どんな助けもありがとうございます。 – richie

+1

@richie、[この回答](http://stackoverflow.com/a/8149216)を参照してください。 – pdoherty926

関連する問題