我正在使用锚链接进行菜单导航,我希望菜单在滚动时显示为活动状态,但如果我转到另一个页面仍然有效。
请检查我收集的代码和我的 wordpress 菜单链接。
this codes worked if the menu links is #section-1 only and not this https://sample.com/#section-1
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var navChildren = $("#top-menu li").children();
var aArray = [];
for (var i = 0; i < navChildren.length; i++) {
var aChild = navChildren[i];
var ahref = $(aChild).attr('href');
aArray.push(ahref);
}
$(window).scroll(function() {
var windowPos = $(window).scrollTop();
var windowHeight = $(window).height();
var docHeight = $(document).height();
for (var i = 0; i < aArray.length; i++) {
var theID = aArray[i];
var secPosition = $(theID).offset().top;
secPosition = secPosition - 230; //135
var divHeight = $(theID).height();
divHeight = divHeight + 90;
if (windowPos >= secPosition && windowPos < (secPosition + divHeight)) {
$("a[href='" + theID + "']").parent().addClass("current-item");
console.log("a[href='" + theID + "']");
} else {
$("a[href='" + theID + "']").parent().removeClass("current-item");
console.log("a[href='" + theID + "']");
}
}
});
});
})(jQuery);
</script>
我的菜单链接示例是:
https ://sample.com/#section-1
https://sample.com/#section-2
https://sample.com/#section-3
等等......
我的控制台错误是这样的:
Uncaught Error: Syntax error, unrecognized expression:
https://pflege.cdemo.me/#section-1
at Function.ea.error (jquery.js?ver=1.12.4-wp:2)
at ea.tokenize (jquery.js?ver=1.12.4-wp:2)
at ea.select (jquery.js?ver=1.12.4-wp:2)
at Function.ea (jquery.js?ver=1.12.4-wp:2)
at Function.a.find (jquery-migrate.min.js?ver=1.4.1:2)
at n.fn.init.find (jquery.js?ver=1.12.4-wp:2)
at n.fn.init.a.fn.find (jquery-migrate.min.js?ver=1.4.1:2)
at a.fn.init.n.fn.init (jquery.js?ver=1.12.4-wp:2)
at new a.fn.init (jquery-migrate.min.js?ver=1.4.1:2)
at n (jquery.js?ver=1.12.4-wp:2)