How can I fix the issue of anchor links not working when implementing scroll snapping in Webflow?

Published on
September 22, 2023

To fix the issue of anchor links not working when implementing scroll snapping in Webflow, you can follow these steps:

  1. Check the link structure: Ensure that the anchor links have the correct structure. The href attribute of the anchor link should match the ID of the element you want to scroll to. For example, if you want to scroll to a section with an ID of "section-1," the link should be <a href="#section-1">.

  2. Add custom code: Webflow automatically generates scroll interactions when you enable scroll snapping. However, it does not handle anchor links correctly by default. To fix this, you can add some custom code.

  3. Identify the scroll snapping container: In your Webflow project, find the element that contains the scroll snapping (usually a section element). Give it a unique class or ID, such as "scroll-container".

  4. Add custom code embed: Go to your website's custom code settings. Open the "Head Code" section, and add the following code between <head> and </head>:

<script>document.addEventListener("DOMContentLoaded", function() {  const links = document.querySelectorAll('a[href^="#"]');    links.forEach(link => {    link.addEventListener("click", (e) => {      e.preventDefault();            const target = document.querySelector(link.getAttribute("href"));      target.scrollIntoView({        behavior: "smooth"      });    });  });});</script>
  1. Publish and test: Save the changes, publish your site, and test the anchor links. They should now work as expected, smoothly scrolling to the specified sections.

Note: If you have added any other custom code or interactions that affect scrolling in your Webflow project, make sure there are no conflicts.

These steps should help you resolve the issue of anchor links not working when implementing scroll snapping in Webflow. Enjoy seamless scrolling with anchor links on your website!

Additional Questions:

  1. How do I enable scroll snapping in Webflow?
  2. What are anchor links in Webflow?
  3. Can I customize the scroll speed when using anchor links in Webflow?