Bootstrap / Bootstrap Advanced Components

Implementing Scrollspy and Affix

This tutorial will guide you in implementing the Scrollspy and Affix components of Bootstrap. These tools enhance user navigation and improve the overall user interface of a websi…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Introduces advanced Bootstrap components such as accordions, scrollspy, and tooltips.

Implementing Scrollspy and Affix

1. Introduction

This tutorial aims at equipping you with the knowledge and skills to implement Scrollspy and Affix components of Bootstrap in your web development projects. These tools enhance the navigation experience of users on your website and generally improve the user interface.

After completing this tutorial, you will be able to:

  • Understand what Scrollspy and Affix are and their use cases
  • Implement Scrollspy for automatic updating of nav targets based on scroll position
  • Implement Affix for making navbars stick at the top of the page when scrolled

Prerequisites

Basic knowledge of HTML, CSS and JavaScript is required. Familiarity with Bootstrap would also be helpful.

2. Step-by-Step Guide

Scrollspy

The Scrollspy component in Bootstrap automatically updates nav targets based on scroll position. It's used for creating a navigation menu that highlights the current section.

Affix

Affix is a jQuery plugin that allows you to position a part of your page always visible, no matter how much you scroll. It's used for creating a sticky navbar.

Tips and Best Practices

  • Ensure that your page has a DOCTYPE.
  • Use scrollspy and affix in a responsive design to enhance user experience.
  • Always test your implementation on various screen sizes for compatibility.

3. Code Examples

Example 1: Scrollspy

<body data-spy="scroll" data-target=".navbar" data-offset="50">

  <!-- Navbar -->
  <nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">  
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="#section1">Section 1</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#section2">Section 2</a>
      </li>
    </ul>
  </nav>

  <!-- Sections -->
  <div id="section1">Content for Section 1</div>
  <div id="section2">Content for Section 2</div>

</body>

In this example, as you scroll down the page, the active link in the navigation bar will change according to the section currently visible in your viewport.

Example 2: Affix

<div data-spy="affix" data-offset-top="60" data-offset-bottom="200">
  <ul>
    <li><a href="#section1">Section 1</a></li>
    <li><a href="#section2">Section 2</a></li>
  </ul>
</div>

In this example, the navigation bar will stick to the top of the page when you scroll down, and it will stop sticking to the top of the page when you reach 200px from the bottom.

4. Summary

You learned what Scrollspy and Affix are, and how to implement them in Bootstrap. Scrollspy updates nav targets based on scroll position, and Affix makes a navbar stick at the top of the page when scrolled.

As next steps, try to implement these features in your projects and explore other Bootstrap components. Be sure to check out the official Bootstrap documentation for more information.

5. Practice Exercises

  1. Create a webpage with 5 sections and implement Scrollspy to highlight the current section in the navigation bar.

  2. Implement Affix on a navbar in a webpage with a lot of content. The navbar should stick to the top when scrolling down and stop sticking when 100px from the bottom.

  3. Combine Scrollspy and Affix in one webpage. The navbar should highlight the current section and stick to the top when scrolling.

Remember, practice makes perfect. Keep coding and exploring new features.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help