ページのさまざまなセクションをスクロールしながら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。
何かエラーが表示されますか? – Dylon
こんにちは、私は取得:Uncaught ReferenceError:$ targetは定義されていません –
私は問題が$ターゲットが厳密なモードが有効になっているグローバル変数として使用されていると思う。私は提案された解決策を投稿しました。参照してください:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Converting_mistakes_into_errors – Dylon