简体   繁体   English

刷新页面后使锚链接保持活动类(平滑的锚滚动)

[英]Keep anchor link active class after refresh the page (smooth anchor scroll)

I need some help how to keep keep current anchor link active after refresh the page. 我需要一些帮助,以在刷新页面后如何使当前锚链接保持活动状态。 Practically, I have this vertical navbar menu ( http://prntscr.com/fz9yct ), by default I added class active to the <a href="#home"> so when we open the page, navbar looks like as you see on screenshot. 实际上,我有这个垂直的导航栏菜单( http://prntscr.com/fz9yct ),默认情况下,我在<a href="#home">添加了活动类,因此当我们打开页面时,导航栏看起来就像你看到的那样在屏幕截图上。

When I scroll, or click on any other navbar list item , everything works good , smooth scroll to the appropriate section and remove class active from from the previous anchor link and add to the appropriate. 当我滚动或单击任何其他导航栏列表项时,一切正常,平滑滚动到适当的部分,并从上一个锚点链接中删除活动的类并添加到适当的地方。

Issue is when I refresh the page class active the class active does not remain where it should be, but it is assigned to the #home anchor link, although the content stays where it was before refresh. 问题是,当我刷新活动的页面类时,活动的类不会保留在应有的位置,而是将其分配给#home锚链接,尽管内容仍保持在刷新之前的位置。 Can you help me how to fix that ? 你能帮我解决这个问题吗?

I tried some solution with $(window).on('beforeunload', function() { but does not work 我尝试使用$(window).on('beforeunload', function() {

 <div class="main-menu-container"> <nav class="main-menu" id="navbar" aria-label="Main Menu"> <ul role="menubar" class="center"> <li class="menubar__item"> <a href="#home" class="active block" title="Home"> <span class="menubar__item--icon block"> SVG ICON HERE </span> <span class="menubar__item--text">Home</span> </a> </li> <li class="menubar__item"> <a href="#about" class="block" title="About"> <span class="menubar__item--icon block"> SVG ICON HERE </span> <span class="menubar__item--text">About</span> </a> </li> <li class="menubar__item"> <a href="#experience" class="block" title="My Experience"> <span class="menubar__item--icon block"> SVG ICON HERE </span> <span class="menubar__item--text">Experience</span> </a> </li> <li class="menubar__item"> <a href="#services" class="block" title="What I provide"> <span class="menubar__item--icon block"> SVG ICON HERE </span> <span class="menubar__item--text">Services</span> </a> </li> <li class="menubar__item"> <a href="#contact" class="block" title="Get in touch"> <span class="menubar__item--icon block"> SVG ICON HERE </span> <span class="text">Contact</span> </a> </li> </ul> <!-- /.menubar end --> </nav> <!-- /.main-menu end --> </div> <!-- / .main-menu-container end --> 

 $(document).ready(function() { $(document).on("scroll", onScroll); $('a[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\\//, '') == this.pathname.replace(/^\\//, '') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); if (target.length && $(this).attr("href") == '#home') { $("html, body").animate({ scrollTop: target.offset().top }, 1000); } else { $("html, body").animate({ scrollTop: target.offset().top + 2 }, 1000); } return false; } }); }); function onScroll(event) { var scrollPos = $(document).scrollTop(); $('#navbar a').each(function() { var currLink = $(this); var refElement = $(currLink.attr("href")); if (refElement.offset().top <= scrollPos && refElement.offset().top + refElement.height() > scrollPos) { $('#navbar ul li a').removeClass("active"); currLink.addClass("active"); } else { currLink.removeClass("active"); } }); } 

Looks like you use that answer and forget to remove class when click. 好像您使用该答案,而单击时忘记删除类。

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

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

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