简体   繁体   English

更改具有多个菜单的滚动上的活动菜单项

[英]Change Active Menu Item on Scroll with Multiple Menus

I have a single page scrolling website and would like to change the active class of my links on both scroll and click. 我只有一个页面滚动网站,并且想更改滚动和单击时链接的活动类别。 I've found some great snippets from this website which do half the job (for those that are interested this also does smooth scrolling): 我从该网站上找到了一些很棒的摘要,它们完成了一半的工作(对于那些感兴趣的人,这也可以平滑滚动):

 $(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(); $('a').each(function () { var currLink = $(this); var refElement = $(currLink.attr("href")); if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { $('a').removeClass("active"); currLink.addClass("active"); } else{ currLink.removeClass("active"); } }); } 

The problem arises in that I have multiple menus that are visible at different screen sizes. 出现问题是因为我有多个菜单,这些菜单在不同的屏幕尺寸下可见。 I'm struggling with a way to change the active class on both menu items simultaneously. 我正在设法同时更改两个菜单项上的活动类。

Any suggestions would be greatly appreciated 任何建议将不胜感激

Solved this myself but I would welcome a cleaner solution. 我自己解决了这个问题,但我欢迎更干净的解决方案。 I simply duplicated the code:` 我只是复制了代码:

 $(document).ready(function () { $(document).on("scroll", onScroll); //smoothscroll $('#nav-minor a[href^="#"]').on('click', function (e) { e.preventDefault(); $(document).off("scroll"); $('#nav-minor 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 scrollPo = $(document).scrollTop(); $('#nav-minor a').each(function () { var currLink = $(this); var refElement = $(currLink.attr("href")); if (refElement.position().top <= scrollPo && refElement.position().top + refElement.height() > scrollPo) { $('#nav-minor a').removeClass("active"); currLink.addClass("active"); } else{ currLink.removeClass("active"); } }); } 

albeit with different ID's for the secondary menu: 尽管辅助菜单具有不同的ID:

 $(document).ready(function () { $(document).on("scroll", onScroll); //smoothscroll $('#nav-main li a[href^="#"]').on('click', function (e) { e.preventDefault(); $(document).off("scroll"); $('#nav-main li').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(); $('#nav-main li a').each(function () { var currItem = $(this); var refElement = $(currItem.attr("href")); if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) { $('#nav-main li a').removeClass("active"); currItem.addClass("active"); } else{ currItem.removeClass("active"); } }); } 

If anyone has a better answer to this would be appreciated. 如果有人对此有更好的答案,将不胜感激。 Purely for academic reasons now :-)` 现在纯粹出于学术原因:-)`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM