Let's create a script that automates the process of generating a Table of Contents for your Webflow website. This script will dynamically create a list of links based on the headings in your content and add scroll-to functionality. It works with CMS and static content. Now, let's delve into the details and understand how this script works. If you need a copy-paste solution, please open the clonable Webflow project.
Make sure you have a Webflow project set up with the necessary content. Identify the container where you want to display the Table of Contents. In our example, we use the <div id="toc"> element.Make sure to structure your content using the appropriate heading tags (h2, h3, h4). These headings will be utilized to generate the Table of Contents. To create a Table of Contents, ensure that your content is contained within an element with the ID <div id="single-article">. If you choose to change the ID name, remember to update the corresponding CSS and JavaScript accordingly.
We begin by adding an event listener to the DOMContentLoaded event. This ensures that the code is executed when the HTML document has finished loading and the DOM is ready for manipulation. Inside this event listener, we retrieve references to the article and tocContainer elements using their respective IDs: "single-article" and "toc".
Next, we define the createTOC function, which is responsible for generating the Table of Contents (TOC) based on the headings found within the article element. This function selects all the h2, h3, and h4 headings within the article and creates a TOC item for each heading. The TOC item is a list item (li) containing an anchor link (a) with the heading title as its text content. The anchor link is assigned an href attribute that points to the corresponding heading's ID. The generated TOC items are appended to a document fragment for efficiency and then inserted into the tocContainer element.
After defining the createTOC function, we check if both the tocContainer and article elements exist. If they do, we call the createTOC function to generate the TOC.
Next, we select all the TOC items and section titles within the content area. These elements will be used to handle click events and highlight the active TOC item.
We define the setActiveItem function, which is responsible for setting the active item in the TOC. This function iterates over all the TOC items and adds the 'active' class to the item whose href attribute matches the target ID. It removes the 'active' class from the rest of the items.
We attach click event listeners to all the TOC items. When a TOC item is clicked, the listener prevents the default link behavior, retrieves the target ID from the clicked item's href attribute, sets the active item using the setActiveItem function, and scrolls to the corresponding section by finding the element with the matching ID and calling scrollIntoView().
Similarly, we attach click event listeners to all the section titles. When a section title is clicked, the listener retrieves the target ID and sets the active item using the setActiveItem function.
We create an IntersectionObserver to track the intersection of the headings with the viewport. When a heading intersects with the viewport, we remove the 'active' class from other TOC items and add the 'active' class to the corresponding TOC item using the heading's ID.
If there are headings present (checked by the condition "h2,h3,h4" !== ""), we select all the headings within the article element and observe them for intersection using the IntersectionObserver.
We can define the offsetAnchor function to handle the anchor position when the page loads or the hash changes. If the hash in the URL is not empty, the function retrieves the target ID from the hash, calculates the offset to scroll to the target element, considering any fixed headers, and scrolls to the target element.
We add event listeners for hash changes and call the offsetAnchor function to handle the initial anchor position when the page loads.
That's it! With the above code implemented on your Webflow website, you'll have a functional and dynamic Table of Contents. Feel free to customize the styling and appearance of the TOC to match your website's design.