Categories: CSSScriptWeb Design

Create Dynamic Navigation Menus (TOC) for Long Content – Anchor.js

Anchor.js is a lightweight JavaScript library that automatically generates a table of contents (TOC) based on the headings within the container you specify.

This library automatically scans for heading elements like <h1>, <h2> and creates a list-based navigation menu with smooth scroll and scrollspy support.

Features:

  • Automatic generation of navigation menus from page headings
  • Active class highlighting that tracks the current section
  • Sponsored
  • Smooth scrolling functionality between page sections
  • Support for multiple heading levels (<h1> through <h6>)
  • Customizable styling options for both menus and headings
  • Adjustable scroll offset positioning
  • Font size and weight customization for different heading levels

How to use it:

1. Download and add Anchor.js to your HTML page.

<script src="anchor.js"></script>

2. Add an empty unordered list where you want the table of contents to appear:

<ul id="toc"></ul>

3. Organize your content with heading elements as follows:

<div class="container">
  <h1>Title 1</h1>
  <p>Section 1...</p>
  <h2>Title 1.1</h2>
  <p>Section 1.1...</p>
  <h1>Title 2</h1>
  <p>Section 2...</p>
</div>

4. Create a new Anchor object, targeting your list and specifying the headings.

Sponsored
const anchor = new Anchor('#toc', {
  contentClass: 'container',
  listHead: ['h1', 'h2'],
});

5. Customize the Anchor with the following options:

const anchor = new Anchor('#toc', {
  contentClass: 'container',
  listHead: ['h1', 'h2'],
  activeClass: 'active',  
  headClass: 'highlight', 
  defaultFontSize: { 
    h1: '48px',
    h2: '36px'
  },
  defaultFontWeight: { 
    h1: 'bolder',
    h2: 'bold'
  },
  offsetTop: 70
});

Changelog:

06/12/2025

  • Refactor Anchor JS and add Vue & React versions

The post Create Dynamic Navigation Menus (TOC) for Long Content – Anchor.js appeared first on CSS Script.

rssfeeds-admin

Share
Published by
rssfeeds-admin

Recent Posts

House fire in Rockford displaces one, cause under investigation

ROCKFORD, Ill. (WTVO) — A house fire Sunday afternoon leaves one adult displaced. The Rockford…

4 hours ago

A robot arm with puppy dog eyes is just one of Lenovo’s new desktop AI concepts

The AI Workmate Concept can move and rotate to accomplish various tasks, but can it…

5 hours ago

The new Yoga 9i 2-in-1 from Lenovo has an angled ‘canvas mode’ for easier note-taking

The magnetic pen case is pulling wedge duty in there. Lenovo has a few new…

5 hours ago

Lenovo’s redesigned ThinkPad Detachable tablet has a bigger screen and legit keyboard

We’ve been waiting five years for this follow-up to the X12 Detachable. | Image: Lenovo…

5 hours ago

Minor injuries reported after crash south of Abilene

TAYLOR COUNTY, Texas (KTAB/KRBC) - A two-vehicle collision occurred south of Abilene Sunday afternoon. The…

6 hours ago

Scream 7 Secures Biggest Box Office Opening Weekend of the Scream Franchise

Scream 7 has enjoyed a huge box office opening weekend, with nearly $100 million secured…

7 hours ago

This website uses cookies.