Eclipsed Table of Contents

You can view all three components (CSS, HTML, Javascript) for this snippet together on Codepen, as well as see a live preview that you can edit on the fly...

You’ll notice on this (and most sites) that the (primary) navigation takes the form of a static menu area appearing in all the pages.

This works well if your site only has a few pages, but can make navigating tricky for visitors if your site is vast. How is it all supposed to fit? If you have a large site, consider also having a dedicated page for the entire table of contents (or site map, as one might call it).

You can, of course, do this with a normal (unordered, or even ordered) HTML list of links, but this actually complicates things in the long run. It’s a far better idea to plan ahead and organize your pages into categories and sections. These can then appear as nested items within an unordered list, making navigation a little easier.

Depending on your site’s ultimate (planned?) size, though, even this can look a bit messy for visitors. My solution, like many people, was a collapsing menu. These are extremely common online, and hide the nested lists unless you click on the headers to reveal their contents.

This shrinks the list and keeps the visitor from feeling overwhelmed when faced with the long list, while letting them check out topical areas that interest them. On this page, I’ll provide a basic option I was able to set up for a collapsing menu with annotations to allow for customizing the code.

There’s probably a lot more that can be done here, too, of course, that I haven’t even begun to learn yet. For now, this table of contents supports three levels of menus with custom bullet points, and also has an optional class to add to items that you haven’t yet completed, labeling them as such.

Please note, though - jquery is required for all this to work. This can be added by pasting:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

...right before </body> on any page that deploys this, Many people already use it on most of their site, of course.

CSS

You apply the class .eclipse to an anchor tag that you'd like to turn into a heading for a nested list, and follow that up with the nested list itself. The rest of the CSS styles and prepares the unordered list for the visitor. While the code will (technically) work without most of this extra CSS, it doesn’t work in a sensical way, and modification are necessary to make this usable for your average visitor. I hope I’ve written decent documentation for the CSS itself below. There are instructions for changing the bullet points used by various levels of the Table of Contents (down to a depth of three), colors, etc. This will, I hope, allow most people to copy and use the code as their own, changing it as necessary.

HTML

The HTML here is almost exactly like you’d expect of a series of nested, unordered lists. When you want to nest a list, place it directly within the existing list item of the top-level list. Just before that (before the <ul> tag for the nested list, that is), you’ll add that anchor tag and apply the .eclipse class to it, turning it into a clickable link that opens the (initially hidden) nested list.

For list items that link to incomplete or future pages, you can add the class .gray. This will gray out the link, to indicate to the visitor that it's just not there yet. If you're using that, be sure to apply the class directly to the list item in question (ie <li class="gray">). Don't apply it to the link/anchor.

Javascript

Include this Javascript, either between <script> tags just before your body tag closes, or in a separate Javascript file linked in the same location. Be sure to also include jquery as described above in the HTML section, too, or none of this will work. This particular magical paradigm (jquery, I mean), is, in fact, key to this whole thing - and specifically, this spell. Again, you'll want to paste:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

...just before your </body> tag. Using that, we’re able to create a sliding visibility effect. When the user clicks on something with the class .eclipse, lists directly following it to become visible, slowly. If you change the name of the class (.eclipse) in the (above) HTML and CSS, be sure to change it here, too! Also, by replacing the value ”slow” with "fast" (or a number followed by s or ms) will change the speed at which the menu opens, if you care that much…