2016-06-29 5 views
0

ページのさまざまなセクションをスクロールしながらnaviagtionクラスを変更しようとしていますが、エラーが発生しています。ここに私の現在のコードは次のとおりです。ワードプレスのテーマでこれを使用しようとしてナビゲーションの変更ページ上のアクティブなクラスjQueryでスクロール

(function($) { 
"use strict"; 

    $(document).ready(function() { 

    $(document).on("scroll", onScroll); 

    //smoothscroll 
    $('a[href^="#"]').on('click', function (e) { 
     e.preventDefault(); 
     $(document).off("scroll"); 

     $('a').each(function() { 
      $(this).removeClass('active'); 
     }) 
     $(this).addClass('active'); 

     var target = this.hash, 
      menu = target; 
     $target = $(target); 
     $('html, body').stop().animate({ 
      'scrollTop': $target.offset().top+2 
     }, 500, 'swing', function() { 
      window.location.hash = target; 
      $(document).on("scroll", onScroll); 
     }); 
    }); 
}); 

function onScroll(event){ 
    var scrollPos = $(document).scrollTop(); 
    $('.menu li a').each(function() { 
     var currLink = $(this); 
     var refElement = $(currLink.attr("href")); 
     if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { 
      $('.menu li a').removeClass("active"); 
      currLink.addClass("active"); 
     } 
     else{ 
      currLink.removeClass("active"); 
     } 
    }); 
} 

    })(jQuery); 

イム。問題の原因となっているコードレイアウトに間違いはありますか?

ありがとうございます。

Scott。

+0

何かエラーが表示されますか? – Dylon

+0

こんにちは、私は取得:Uncaught ReferenceError:$ targetは定義されていません –

+0

私は問題が$ターゲットが厳密なモードが有効になっているグローバル変数として使用されていると思う。私は提案された解決策を投稿しました。参照してください:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Converting_mistakes_into_errors – Dylon

答えて

0

問題は、それを定義する前に、strictモードを有効にして、$targetを使用していることかもしれません。宣言リストに$targetを初期化してください(コンマでリストに追加してください):

(function($) { 
"use strict"; 

    $(document).ready(function() { 

    $(document).on("scroll", onScroll); 

    //smoothscroll 
    $('a[href^="#"]').on('click', function (e) { 
     e.preventDefault(); 
     $(document).off("scroll"); 

     $('a').each(function() { 
      $(this).removeClass('active'); 
     }) 
     $(this).addClass('active'); 

     var target = this.hash, 
      menu = target, 
      $target = $(target); 
     $('html, body').stop().animate({ 
      'scrollTop': $target.offset().top+2 
     }, 500, 'swing', function() { 
      window.location.hash = target; 
      $(document).on("scroll", onScroll); 
     }); 
    }); 
}); 

function onScroll(event){ 
    var scrollPos = $(document).scrollTop(); 
    $('.menu li a').each(function() { 
     var currLink = $(this); 
     var refElement = $(currLink.attr("href")); 
     if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { 
      $('.menu li a').removeClass("active"); 
      currLink.addClass("active"); 
     } 
     else{ 
      currLink.removeClass("active"); 
     } 
    }); 
} 

    })(jQuery); 
+0

これは多くのおかげで、それはターゲットの問題を修正したようです。しかし今はスクロール機能に関して異なるエラーが発生しています。 Im取得:jUncaughtエラー:構文エラー、認識されない式:http:// localhost:8888/chase任意のアイデアですか?あなたのお時間をありがとうございました。 –

+0

申し訳ありません最後のコメントを無視してください.Iidsが設定されていない行があるためでした。あなたの助けをもう一度ありがとう。 –

関連する問題