scrollspy bootstrap 4 code example

Example 1: Bootstrap 4 Scrollspy

<!-- The scrollable area -->
<body data-spy="scroll" data-target=".navbar" data-offset="50">

<!-- The navbar - The <a> elements are used to jump to a section in the scrollable area -->
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
...
  <ul class="navbar-nav">
    <li><a href="#section1">Section 1</a></li>
    ...
</nav>

<!-- Section 1 -->
<div id="section1">
  <h1>Section 1</h1>
  <p>Try to scroll this page and look at the navigation bar while scrolling!</p>
</div>
...

</body>

Example 2: scrollspy bootstrap 4

<nav id="navbar-example2" class="navbar navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <ul class="nav nav-pills">
    <li class="nav-item">
      <a class="nav-link" href="#fat">@fat</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" href="#mdo">@mdo</a>
    </li>
    <li class="nav-item dropdown">
      <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
      <div class="dropdown-menu">
        <a class="dropdown-item" href="#one">one</a>
        <a class="dropdown-item" href="#two">two</a>
        <div role="separator" class="dropdown-divider"></div>
        <a class="dropdown-item" href="#three">three</a>
      </div>
    </li>
  </ul>
</nav>
<div data-spy="scroll" data-target="#navbar-example2" data-offset="0">
  <h4 id="fat">@fat</h4>
  <p>...</p>
  <h4 id="mdo">@mdo</h4>
  <p>...</p>
  <h4 id="one">one</h4>
  <p>...</p>
  <h4 id="two">two</h4>
  <p>...</p>
  <h4 id="three">three</h4>
  <p>...</p>
</div>

Example 3: bootstrap scrollspy

<!-- Bootstrap 5 Scrollspy -->
<nav id="navbar-example2" class="navbar navbar-light bg-light px-3">
   <a class="navbar-brand" href="#">Navbar</a>
   <ul class="nav nav-pills">
      <li class="nav-item">
         <a class="nav-link" href="#fat">@fat</a>
      </li>
      <li class="nav-item">
         <a class="nav-link" href="#mdo">@mdo</a>
      </li>
      <li class="nav-item dropdown">
         <a
            class="nav-link dropdown-toggle"
            data-bs-toggle="dropdown"
            href="#"
            role="button"
            aria-expanded="false"
            >Dropdown</a
         >
         <ul class="dropdown-menu dropdown-menu-end">
            <li><a class="dropdown-item" href="#one">one</a></li>
            <li><a class="dropdown-item" href="#two">two</a></li>
            <li><hr class="dropdown-divider" /></li>
            <li><a class="dropdown-item" href="#three">three</a></li>
         </ul>
      </li>
   </ul>
</nav>
<div
   data-bs-spy="scroll"
   data-bs-target="#navbar-example2"
   data-bs-offset="0"
   tabindex="0"
>
   <h4 id="fat">@fat</h4>
   <p>...</p>
   <h4 id="mdo">@mdo</h4>
   <p>...</p>
   <h4 id="one">one</h4>
   <p>...</p>
   <h4 id="two">two</h4>
   <p>...</p>
   <h4 id="three">three</h4>
   <p>...</p>
</div>

Tags:

Misc Example