gracemoore Posted Saturday at 04:36 PM Posted Saturday at 04:36 PM 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!
Solution paul2009 Posted Sunday at 04:04 PM Solution Posted Sunday at 04:04 PM 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 ⬇️ gracemoore 1 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.
paul2009 Posted Monday at 05:35 PM Posted Monday at 05:35 PM @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.
gracemoore Posted Monday at 06:13 PM Author Posted Monday at 06:13 PM 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?
paul2009 Posted Monday at 07:12 PM Posted Monday at 07:12 PM 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment