Creating a Table of Contents and implement scroll functionality

In this article, I will explain how to create an automatic scrolling function. This feature allows users to automatically scroll to the relevant section when they click on an item in the table of contents.

Although DOM manipulations related to scrolling may seem simple at first glance, they can be difficult for beginners. In this article, we will share our experience and trial-and-error process in implementing this scrolling function.

Development Environment

BACKEND: Django

FRONTEND: Django templates and jQuery

Utilized Django and JS Packages

This time, we will only be using jQuery. We won't install any special Django packages for our purposes.

While there are many articles on the web about implementing a Table of Contents and many TOC modules written in Vanilla JS and jQuery, I found that some of these packages were more difficult to set up than I expected, and some did not work in the environment of this site. Therefore, I concluded that it would be faster to create the TOC myself.

Project structure

Development Process

1. Basic Django setup

This srticle assumes that you already have a basic setup of Django. If you need a basic setup of Django using Docker, click here.

2. Writing the codes

3. Create dummy content in .container

4. Create a Table Of Contents

5. Implementing the Scroll Function

Key points of this JS code

This JavaScript code first gets the href attribute (ID of the link destination) that the user clicked on. It then measures the distance between the top of the main part and the link destination, and scrolls the measured distance using the Animate method.

There are two key points to note:

  • 1. When scrolling, it is necessary to obtain the distance from the top of the element to be scrolled to the link destination. In this case, the element to be scrolled is `.container`, not the window or body tag.
  • 2. The Animate method starts from `.container`, not HTML or body.

Since the header is fixed to the top of the window, measuring the scroll distance from the window or body will not scroll correctly. Therefore, the distance is measured from the container class.

The following code is especially important:

$(".container").animate({scrollTop : position}, speed, "swing");

Here is an explanation of this code:

  1. $(".container"): Uses the jQuery selector to select elements with class `.container`.
  2. animate(): A jQuery method that applies an animation effect to the selected element.
  3. {scrollTop : position}: Specifies the CSS property to animate and the final value. This example modifies the scroll position (`scrollTop`) and moves it to the value specified by the variable called `position`.
  4. speed: Specifies the animation speed in milliseconds. For example, 1000 will complete the animation in 1 second.
  5. "swing": Specifies the animation easing type. `"swing"` is jQuery's default easing type, which accelerates and decelerates at the beginning and end of the animation. Another option is `"linear"`, which means the animation progresses at a constant speed.

All of this code will move the scroll position of the .container element to position with an animation effect, the speed of the animation will be specified by speed, and the easing type will be "swing".

Now, it's done!

If you're struggling with creating a table of contents in your blog, this article may provide some helpful guidance. Feel free to use it as a reference!