rx-36

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 is ubiquitous on the web and seems straightforward, 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

Django and JavaScript Packages Used

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

While numerous vanilla JavaScript and jQuery-based Table of Contents modules exist online, I found some overly complicated to set up, and others didn't work well in this site's environment. Therefore, creating a custom Table of Contents proved more efficient.

Project structure

Page design

Development Process

  1. Basic Django setup
  2. Write the code
  3. Add dummy content to the .container
  4. Build the Table of Contents
  5. Implement the scroll function

1. Basic Django setup

This article assumes that you already have a basic setup of Django. If you need help setting up Django with Docker, please refer to the article "Basic setup for running Django with Docker".

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.

Two key points to remember:

  • 1. When scrolling, you need to measure the distance from the top of the scrollable element to the target 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, measuring scroll distance from the window or body won't work correctly. Instead, measure from the .container element.

The following code is especially important:

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

Here's what each part does:

  • - `$(".container")`: Uses the jQuery selector to select elements with class `.container`.
  • - `animate()`: A jQuery method that applies an animation effect to the selected element.
  • - `{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`.
  • - `speed`: Specifies the animation speed in milliseconds. For example, 1000 will complete the animation in 1 second.
  • - `"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.

This code moves 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".

That's it!

If you're looking to add a table of contents with smooth scrolling to your Django project, I hope this guide helps!