Jump to content

Make Anchor Links In Main Navigation Bold When Clicked/Active

Go to solution Solved by paul2009,

Recommended Posts

Posted

Site URL: https://torvic-design-solutions.squarespace.com/

Site Password: TORVIC

I am building a single page website and am using anchor links in the main navigation to jump to the different sections. However, I am struggling to write a piece of code to get these anchor links in the main nav to remain bold once they are clicked.

Any suggestions on how to get the anchor links to appear in bold once clicked (like in the screenshot attached)?

Many thanks in advance!

Screenshot 2024-11-30 at 16.36.19.png

  • Solution
Posted
22 hours ago, gracemoore said:

I am building a single page website and am using anchor links in the main navigation to jump to the different sections. However, I am struggling to write a piece of code to get these anchor links in the main nav to remain bold once they are clicked.

If you add IDs directly to each page section, you can then add some JavaScript to detect the current scroll position and then apply a CSS class to the corresponding navigation link that increases the weight of the font. Let me know if you need help once you've added the links.

Did this help? Please give feedback by clicking an icon below  ⬇️

 

Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. 
Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links.
Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional.

Posted

@gracemoore Here's an example of some JavaScript to do this. Add to the Code Injection FOOTER panel.

<!-- Highlight active section on Squarespace 7.1 --------------------->
<!-- Copyright Soundfocus Digital [https://sf.digital] --------------->
<!-- Use freely in your code injection. Do NOT re-publish.------------>
<script>  
  const navLinks = document.querySelectorAll('.header-nav-item a');
  const sections = document.querySelectorAll('section');
  function highlightNavLink() {
    let scrollPosition = window.scrollY + window.innerHeight / 2;
    sections.forEach((section, index) => {
      const sectionTop = section.offsetTop;
      const sectionBottom = sectionTop + section.offsetHeight;
      if (scrollPosition >= sectionTop && scrollPosition < sectionBottom) {
        navLinks.forEach(link => link.classList.remove('active'));
        navLinks[index].classList.add('active');
      }
    });
  }
  // Listen for scroll events
  window.addEventListener('scroll', highlightNavLink);
  // Trigger function on page load
  highlightNavLink();
</script>

Did this help? Please give feedback by clicking an icon below  ⬇️

Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. 
Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links.
Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional.

Posted

Many thanks for your help Paul! I have added this JavaScript to the code injection footer, do I also need to add some CSS code to make the links in the navigation bar turn bold?

Posted

Yes, that would help 🤦‍♂️.

.header-nav-item a.active {
  font-weight: bold;
}

 

Me: I'm Paul, a SQSP user for >18 yrs & Circle Leader since 2017. I value honesty, transparency, diversity and good design ♥.
Work: Founder of SF.DIGITAL. We provide high quality original extensions to supercharge your Squarespace website. 
Content: Views and opinions are my own. Links in my posts may refer to my own SF.DIGITAL products or may be affiliate links.
Forum advice is completely free. You can thank me by selecting a feedback emoji. Buying a coffee is generous but optional.

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.