Creating a Table of Contents and implement scroll functionality

This article explains how to create a smooth scrolling function. Smooth scrolling allows a user to click a link that targets a different section within the same page, causing the browser to smoothly animate the scroll to that target destination.

While scrolling functionality seems trivial because it's so ubiquitous on the web, it can be surprisingly challenging for beginners to implement. This article will guide you through the implementation process.

Development Environment

BACKEND: Django

FRONTEND: Django templates and jQuery

Utilized Django and JS Packages

We'll only be using jQuery for this implementation; no special Django packages are required.

While numerous Vanilla JS and jQuery-based Table of Contents modules exist online, I found that some were more complicated to set up than expected, and others didn't work well within this site's environment. Therefore, I decided it would be faster to create a custom Table of contents.

Project structure

Page design

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!