Div scrollable bootstrap display from bottom to top năm 2024

How TO - Scroll Back To Top Button

Learn how to create a "scroll back to top" button with CSS.

Try it Yourself »

How To Create a Scroll To Top Button

Step 1) Add HTML:

Create a button that will take the user to the top of the page when clicked on:

Example

<button title="topFunction()" id="myBtn" title="Go to top">Top</button>

Step 2) Add CSS:

Style the button:

Example

myBtn {

display: none; /* Hidden by default */ position: fixed; /* Fixed/sticky position */ bottom: 20px; /* Place the button at the bottom of the page */ right: 30px; /* Place the button 30px from the right */ z-index: 99; /* Make sure it does not overlap */ border: none; /* Remove borders */ outline: none; /* Remove outline */ background-color: red; /* Set a background color */ color: white; /* Text color */ cursor: pointer; /* Add a mouse pointer on hover */ padding: 15px; /* Some padding */ border-radius: 10px; /* Rounded corners */ font-size: 18px; /* Increase font size */ }

myBtn:hover {

background-color:

555; /* Add a dark-grey background on hover */

}

Step 3) Add JavaScript:

Example

// Get the button: let mybutton = document.getElementById("myBtn");

// When the user scrolls down 20px from the top of the document, show the button window.onscroll = function() {scrollFunction()};

function scrollFunction() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { mybutton.style.display = "block"; } else { mybutton.style.display = "none"; } }

// When the user clicks on the button, scroll to the top of the document function topFunction() { document.body.scrollTop = 0; // For Safari document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera }

Try it Yourself »

★ +1

W3schools Pathfinder

Track your progress - it's free!

I also did a small example here, adapting to use jQuery: //eduardojauch.outsystemscloud.com/ScrollBarTopAndBottom/Home.aspx?_ts=636391644021802422

The OML is attached.

NOTICE: I forgot to mention before, this only works if you fix the width of both internal DIVs, or if you put the same content inside both div's. I used a jQuery approach, so, when the document is ready, I set the width of the inner div (fake) with the width of the real inner div.

The overflow-y property specifies whether to clip the content, add a scroll bar, or display overflow content of a block-level element, when it overflows at the top and bottom edges.

Tip: Use the overflow-x property to determine clipping at the left and right edges.

Show demo ❯

Default value: visible Inherited: no Animatable: no. Read about animatable Version: CSS3 JavaScript syntax: object.style.overflowY="scroll" Try it

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -ms-, specify the first version that worked with a prefix.

Property overflow-y 4.0 9.0 8.0 -ms- 1.5 3.0 9.5

CSS Syntax

overflow-y: visible|hidden|scroll|auto|initial|inherit;

Property Values

Value Description Demo visible The content is not clipped, and it may be rendered outside the content box. This is default Demo ❯ hidden The content is clipped - and no scrolling mechanism is provided Demo ❯ scroll The content is clipped and a scrolling mechanism is provided Demo ❯ auto Should cause a scrolling mechanism to be provided for overflowing boxes Demo ❯ initial Sets this property to its default value. Read about initial inherit Inherits this property from its parent element. Read about inherit

When successfully implemented, your nav or list group will update accordingly, moving the <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

4 class from one item to the next based on their associated targets.

Scrollable containers and keyboard access

If you’re making a scrollable container (other than the <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

1), be sure to have a <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

6 set and <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

7 applied to it—alongside a <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

8 to ensure keyboard access.

Example in navbar

Scroll the area below the navbar and watch the active class change. The dropdown items will be highlighted as well. <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=" # scrollspyHeading1">First</a> </li> <li class="nav-item"> <a class="nav-link" href=" # scrollspyHeading2">Second</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"> <li><a class="dropdown-item" href=" # scrollspyHeading3">Third</a></li> <li><a class="dropdown-item" href=" # scrollspyHeading4">Fourth</a></li> <li><hr class="dropdown-divider"></li> <li><a class="dropdown-item" href=" # scrollspyHeading5">Fifth</a></li> </ul> </li> </ul> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example2" data-bs-offset="0" class="scrollspy-example" tabindex="0"> <h4 id="scrollspyHeading1">First heading</h4> <p>...</p> <h4 id="scrollspyHeading2">Second heading</h4> <p>...</p> <h4 id="scrollspyHeading3">Third heading</h4> <p>...</p> <h4 id="scrollspyHeading4">Fourth heading</h4> <p>...</p> <h4 id="scrollspyHeading5">Fifth heading</h4> <p>...</p> </div>

Example with nested nav

Scrollspy also works with nested <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

9s. If a nested <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

9 is <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

4, its parents will also be <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

4. Scroll the area next to the navbar and watch the active class change. <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

Example with list-group

Scrollspy also works with <div id="list-example" class="list-group"> <a class="list-group-item list-group-item-action" href=" # list-item-1">Item 1</a> <a class="list-group-item list-group-item-action" href=" # list-item-2">Item 2</a> <a class="list-group-item list-group-item-action" href=" # list-item-3">Item 3</a> <a class="list-group-item list-group-item-action" href=" # list-item-4">Item 4</a> </div> <div data-bs-spy="scroll" data-bs-target=" # list-example" data-bs-offset="0" class="scrollspy-example" tabindex="0"> <h4 id="list-item-1">Item 1</h4> <p>...</p> <h4 id="list-item-2">Item 2</h4> <p>...</p> <h4 id="list-item-3">Item 3</h4> <p>...</p> <h4 id="list-item-4">Item 4</h4> <p>...</p> </div>

3s. Scroll the area next to the list group and watch the active class change. <div id="list-example" class="list-group"> <a class="list-group-item list-group-item-action" href=" # list-item-1">Item 1</a> <a class="list-group-item list-group-item-action" href=" # list-item-2">Item 2</a> <a class="list-group-item list-group-item-action" href=" # list-item-3">Item 3</a> <a class="list-group-item list-group-item-action" href=" # list-item-4">Item 4</a> </div> <div data-bs-spy="scroll" data-bs-target=" # list-example" data-bs-offset="0" class="scrollspy-example" tabindex="0"> <h4 id="list-item-1">Item 1</h4> <p>...</p> <h4 id="list-item-2">Item 2</h4> <p>...</p> <h4 id="list-item-3">Item 3</h4> <p>...</p> <h4 id="list-item-4">Item 4</h4> <p>...</p> </div>

Usage

Via data attributes

To easily add scrollspy behavior to your topbar navigation, add <div id="list-example" class="list-group"> <a class="list-group-item list-group-item-action" href=" # list-item-1">Item 1</a> <a class="list-group-item list-group-item-action" href=" # list-item-2">Item 2</a> <a class="list-group-item list-group-item-action" href=" # list-item-3">Item 3</a> <a class="list-group-item list-group-item-action" href=" # list-item-4">Item 4</a> </div> <div data-bs-spy="scroll" data-bs-target=" # list-example" data-bs-offset="0" class="scrollspy-example" tabindex="0"> <h4 id="list-item-1">Item 1</h4> <p>...</p> <h4 id="list-item-2">Item 2</h4> <p>...</p> <h4 id="list-item-3">Item 3</h4> <p>...</p> <h4 id="list-item-4">Item 4</h4> <p>...</p> </div>

4 to the element you want to spy on (most typically this would be the <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

1). Then add the <div id="list-example" class="list-group"> <a class="list-group-item list-group-item-action" href=" # list-item-1">Item 1</a> <a class="list-group-item list-group-item-action" href=" # list-item-2">Item 2</a> <a class="list-group-item list-group-item-action" href=" # list-item-3">Item 3</a> <a class="list-group-item list-group-item-action" href=" # list-item-4">Item 4</a> </div> <div data-bs-spy="scroll" data-bs-target=" # list-example" data-bs-offset="0" class="scrollspy-example" tabindex="0"> <h4 id="list-item-1">Item 1</h4> <p>...</p> <h4 id="list-item-2">Item 2</h4> <p>...</p> <h4 id="list-item-3">Item 3</h4> <p>...</p> <h4 id="list-item-4">Item 4</h4> <p>...</p> </div>

6 attribute with the ID or class of the parent element of any Bootstrap <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

9 component. body { position: relative; } <body data-bs-spy="scroll" data-bs-target=" # navbar-example"> ... <div id="navbar-example"> <ul class="nav nav-tabs" role="tablist"> ... </ul> </div> ... </body>

Via JavaScript

After adding <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column align-items-stretch p-3"> <a class="navbar-brand" href="#">Navbar</a> <nav class="nav nav-pills flex-column"> <a class="nav-link" href=" # item-1">Item 1</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-1-1">Item 1-1</a> <a class="nav-link ms-3 my-1" href=" # item-1-2">Item 1-2</a> </nav> <a class="nav-link" href=" # item-2">Item 2</a> <a class="nav-link" href=" # item-3">Item 3</a> <nav class="nav nav-pills flex-column"> <a class="nav-link ms-3 my-1" href=" # item-3-1">Item 3-1</a> <a class="nav-link ms-3 my-1" href=" # item-3-2">Item 3-2</a> </nav> </nav> </nav> <div data-bs-spy="scroll" data-bs-target=" # navbar-example3" data-bs-offset="0" tabindex="0"> <h4 id="item-1">Item 1</h4> <p>...</p> <h5 id="item-1-1">Item 1-1</h5> <p>...</p> <h5 id="item-1-2">Item 1-2</h5> <p>...</p> <h4 id="item-2">Item 2</h4> <p>...</p> <h4 id="item-3">Item 3</h4> <p>...</p> <h5 id="item-3-1">Item 3-1</h5> <p>...</p> <h5 id="item-3-2">Item 3-2</h5> <p>...</p> </div>

0 in your CSS, call the scrollspy via JavaScript: var scrollSpy = new bootstrap.ScrollSpy(document.body, { target: ' # navbar-example' })

Resolvable ID targets required

Navbar links must have resolvable id targets. For example, a <div id="list-example" class="list-group"> <a class="list-group-item list-group-item-action" href=" # list-item-1">Item 1</a> <a class="list-group-item list-group-item-action" href=" # list-item-2">Item 2</a> <a class="list-group-item list-group-item-action" href=" # list-item-3">Item 3</a> <a class="list-group-item list-group-item-action" href=" # list-item-4">Item 4</a> </div> <div data-bs-spy="scroll" data-bs-target=" # list-example" data-bs-offset="0" class="scrollspy-example" tabindex="0"> <h4 id="list-item-1">Item 1</h4> <p>...</p> <h4 id="list-item-2">Item 2</h4> <p>...</p> <h4 id="list-item-3">Item 3</h4> <p>...</p> <h4 id="list-item-4">Item 4</h4> <p>...</p> </div>

9 must correspond to something in the DOM like body { position: relative; }

0.

Non-visible target elements ignored

Target elements that are not visible will be ignored and their corresponding nav items will never be highlighted.

Methods

refresh

When using scrollspy in conjunction with adding or removing of elements from the DOM, you’ll need to call the refresh method like so: var dataSpyList = [].slice.call(document.querySelectorAll('[data-bs-spy="scroll"]')) dataSpyList.forEach(function (dataSpyEl) { bootstrap.ScrollSpy.getInstance(dataSpyEl) .refresh() })

dispose

Destroys an element’s scrollspy. (Removes stored data on the DOM element)

getInstance

Static method which allows you to get the scrollspy instance associated with a DOM element var scrollSpyContentEl = document.getElementById('content') var scrollSpy = bootstrap.ScrollSpy.getInstance(scrollSpyContentEl) // Returns a Bootstrap scrollspy instance

getOrCreateInstance

Static method which allows you to get the scrollspy instance associated with a DOM element, or create a new one in case it wasn’t initialised var scrollSpyContentEl = document.getElementById('content') var scrollSpy = bootstrap.ScrollSpy.getOrCreateInstance(scrollSpyContentEl) // Returns a Bootstrap scrollspy instance

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to body { position: relative; }

1, as in body { position: relative; }

2.

How do I make a div stick to the top when scrolling?

Apply the sticky property to a div in the CSS file. Here, the property position is set to the sticky to the specified element making the div named “sticky” fixed after you scroll to that div. Apply the position using top, bottom, left, or right properties according to position where it should be fixed.nullHow to make div fixed after scroll to that div ? - GeeksforGeekswww.geeksforgeeks.org › how-to-make-div-fixed-after-scroll-to-that-divnull

How do I scroll to the top of a scrollable div?

To scroll to an element inside a div using JavaScript, you can use the `scrollTop` property of the parent div, setting it to the target element's `offsetTop`. This adjustment allows you to smoothly navigate to a specific element within a scrollable container without using a full-page scroll.nullHow to scroll to an element inside a div using javascript? - GeeksforGeekswww.geeksforgeeks.org › how-to-scroll-to-an-element-inside-a-div-using-j...null

How do I make a div move up and down when I m scrolling the page?

Here are some of the most popular methods for moving a div when scrolling: 1. JavaScript and CSS: This method involves using JavaScript to add an event listener to the scroll event, and then using CSS to manipulate the position of the div based on the scroll position.nullWhat is the best way to move a div when scrolling? - Quorawww.quora.com › What-is-the-best-way-to-move-a-div-when-scrollingnull

How to make a scrollable div in Bootstrap?

Use the . overflow-auto class if you want a scroll in your div. This is an example of using . overflow-auto on an element with set width and height dimensions.nullBootstrap Scroll div - free examples & tutorialmdbootstrap.com › docs › standard › extended › scroll-divnull

Chủ đề