Open In App

Bootstrap 5 Scrollspy Methods

Last Updated : 28 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Bootstrap5 Scrollspy Methods are used to control the Scrollspy element with the help of Javascript and perform operations like disposing, getting or creating the Scrollspy instance, refreshing the element, etc along with finding out useful data about the widget within Javascript.

Bootstrap 5 Scrollspy Methods: The Scrollspy component comes with the following methods:

  • dispose() method: This function is used to destroy an element’s scrollspy. Basically, it removes the stored data of the DOM element.
  • getInstance() method: This function is a static method that is used to get the instance of the scrollspy associated with the given DOM element.
  • getOrCreateInstance() method: This function is also a static method to get the scrollspy instance of the associated DOM element or used to create the scrollspy instance if in case it wasn’t initialized.
  • refresh() method: This function is used to refresh the scrollspy instance. It is used when you are adding or removing elements in the DOM.

Syntax:

  • To get/create the instance of the Scrollspy element:
// For getting the instance
bootstrap.ScrollSpy.getInstance(element)

// For creating and getting the instance
bootstrap.ScrollSpy.getOrCreateInstance(element) 
  • To perform dispose/refresh method:
scrollSpyInstance.dispose() // to dispose
scrollSpyInstance.refresh() // to refresh

 

Example 1: In this example, we have a scrollspy element and we use the getOrCreateInstance method to create an instance of the element and then obtain that instance and its properties on the console. Here, we haven’t used the dispose method to dispose the instances.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="utf-8" />
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1" />
    <title>
        Bootstrap 5 Scrollspy Methods
    </title>
    <link href=
          rel="stylesheet"
          integrity=
"sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" 
          crossorigin="anonymous" />
    <script src=
            integrity=
"sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
            crossorigin="anonymous">
    </script>
</head>
  
<body>
  
    <div class="container">
        <h1 class="text-success">
              GeeksforGeeks
          </h1>
        <h4>
              Bootstrap 5 Scrollspy Methods
          </h4>
        <h5 class="mt-5">
              Top GFG Articles For You: 
          </h5>
  
        <div class="row my-3 bg-dark text-light p-3">
            <div class="col-2">
                <div id="my-nav" class="list-group">
                    <a class= "list-group-item 
                               list-group-item-action" 
                        href="#e1">
                            Article 1
                    </a>
                    <a class= "list-group-item 
                               list-group-item-action" 
                        href="#e2">
                            Article 2
                    </a>
                    <a class= "list-group-item 
                               list-group-item-action" 
                        href="#e3">
                            Article 3
                    </a>
                    <a class= "list-group-item 
                               list-group-item-action" 
                        href="#e4">
                            Article 4
                    </a>
                </div>
            </div>
            <div class="col-6">
                <div style="height: 200px; 
                            overflow-y: scroll;" 
                     id="scrollSpy" 
                     data-bs-spy="scroll"
                     data-bs-target="my-nav" 
                     data-bs-smooth-scroll="true" 
                     tabindex="0">
                    <h4 id="e1">
                        How to Create a QR Code – 4 Ways to
                          Generate Dynamic QR Code For Free
                    </h4>
                    <p>
                        Ever since UPI payment was launched in
                          India, more than 50% of people have 
                           relied on the QR Code payment system.
                        Well, this is one of the examples of 
                          how people heavily depend on QR codes 
                          to simplify their tasks. Previously, 
                        QR codes weren’t popular as mobile 
                          phones didn’t have the feature that has 
                          native QR scanning capabilities.
                    </p>
                    <h4 id="e2" 
                        class="mt-5">
                        7 Reasons to Update Your LinkedIn
                          Profile in 2023
                    </h4>
                    <p>
                        Updating your social media handles 
                          regularly is a must to stay afloat 
                          in the digital world. LinkedIn is one 
                        such platform that is coming up with 
                          various new features every now and then, 
                          so it is apt to say that if you are a
                        LinkedIn user and want to leverage the
                          potential of this platform, then you have
                          to keep your profile updated.
                    </p>
                    <h4 id="e3" 
                        class="mt-5">
                        Puzzle | The Alien Extinction Riddle
                    </h4>
                    <p>
                        An alien visits Earth one day. Each alien
                          accomplishes one of the following four 
                          actions every day, with equal likelihood:
                          Kill himself, Do nothing, Divide into two 
                        aliens (while killing itself), Divided 
                          itself into three aliens (while killing 
                          itself). What is the probability that
                        the alien species eventually die out entirely?
                    </p>
                    <h4 id="e4" 
                        class="mt-5">
                        Maximum triplets containing atleast one
                          x and one y
                    </h4>
                    <p>
                        Given counts of x, y, and z, the task is
                          to find the max number of triplets that 
                          can be made from the count of x, y, 
                        and z such that one triplet contains at 
                          least one x and at least one y. It is 
                        not necessary to use all x, y, and z.
                    </p>
                </div>
            </div>
        </div>
    </div>
  
    <script>
        const scrollSpyElement = 
                   bootstrap.ScrollSpy.getOrCreateInstance(
                  document.querySelector("#scrollSpy"));
        console.log(scrollSpyElement);
    </script>
</body>
  
</html>


Output:

 

Example 2: In this example, we use the getOrCreateInstance method to get the instance and then dispose the instance using the dispose method.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="utf-8" />
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1" />
    <title>
        Bootstrap 5 Scrollspy Methods
    </title>
    <link href=
          rel="stylesheet" 
          integrity=
"sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" 
          crossorigin="anonymous" />
    <script src=
            integrity=
"sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" 
            crossorigin="anonymous">
    </script>
</head>
  
<body>
  
    <div class="container">
        <h1 class="text-success">
              GeeksforGeeks
          </h1>
        <h4>
              Bootstrap 5 Scrollspy Methods
          </h4>
        <h5 class="mt-5">
              Top GFG Articles For You: 
          </h5>
  
        <div class="row my-3 bg-dark 
                    text-light p-3">
            <div class="col-2">
                <div id="my-nav" 
                     class="list-group">
                    <a class="list-group-item 
                              list-group-item-action" 
                       href="#e1">
                        Article 1
                    </a>
                    <a class="list-group-item 
                              list-group-item-action" 
                       href="#e2">
                        Article 2
                    </a>
                    <a class="list-group-item 
                              list-group-item-action" 
                       href="#e3">
                        Article 3
                    </a>
                    <a class="list-group-item 
                              list-group-item-action" 
                       href="#e4">
                        Article 4
                    </a>
                </div>
            </div>
            <div class="col-6">
                <div style="height: 200px; 
                            overflow-y: scroll;" 
                     id="scrollSpy" 
                     data-bs-spy="scroll" 
                     data-bs-target="my-nav"
                     data-bs-smooth-scroll="true" 
                     tabindex="0">
                    <h4 id="e1">
                        How to Create a QR Code – 4 Ways to 
                        Generate Dynamic QR Code For Free
                    </h4>
                    <p>
                        Ever since UPI payment was launched
                          in India, more than 50% of people 
                          have relied on the QR Code payment 
                          system.Well, this is one of the 
                        examples of how people heavily depend 
                          on QR codes to simplify their tasks.
                          Previously, QR codes weren’t popular
                          as mobile phones didn’t have the 
                          feature that has native QR 
                        scanning capabilities.
                    </p>
                    <h4 id="e2" class="mt-5">
                        7 Reasons to Update Your LinkedIn
                        Profile in 2023
                    </h4>
                    <p>
                        Updating your social media handles 
                          regularly is a must to stay afloat 
                          in the digital world. LinkedIn is one 
                          such platform that is coming up with 
                          various new features every now and then,
                        so it is apt to say that if you are 
                        a LinkedIn user and want to leverage 
                          the potential of this platform, then 
                          you have to keep your profile updated.
                    </p>
                    <h4 id="e3" class="mt-5">
                        Puzzle | The Alien Extinction Riddle
                    </h4>
                    <p>
                        An alien visits Earth one day. Each 
                          alien accomplishes one of the following
                          four actions every day, with equal 
                          likelihood: Kill himself, Do nothing,
                          Divide into two aliens (while killing 
                          itself), Divided tself into three aliens 
                          (while killing itself). What is the 
                          probability that the alien species
                        eventually die out entirely?
                    </p>
                    <h4 id="e4" 
                        class="mt-5">
                        Maximum triplets containing atleast 
                        one x and one y
                    </h4>
                    <p>
                        Given counts of x, y, and z, the task is to
                        find the maximum  number of triplets that 
                        can be made from the count of x, y, and z 
                        such that one triplet contains at least 
                          one x and at least one y. It is not 
                           necessary to use all x, y, and z.
                    </p>
                </div>
            </div>
        </div>
    </div>
  
    <script>
        const scrollSpyElement = 
        bootstrap.ScrollSpy.getOrCreateInstance(
            document.querySelector("#scrollSpy"));
        scrollSpyElement.dispose();
        console.log(scrollSpyElement);
    </script>
</body>
  
</html>


Output: From the output, the dispose method does not affect the scrollspy element but removes all the stored data of that element, and hence in output we have all the values of the scrollspy object set to null.

 

Reference: https://getbootstrap.com/docs/5.2/components/scrollspy/#methods



Previous Article
Next Article

Similar Reads

Bootstrap 5 Scrollspy Options
Bootstrap 5 Scrollspy is an automatic navigation mechanism that automatically updates the scroll position. When we click on the link it automatically scrolls down to the currently active element. Bootstrap 5 Scrollspy Options: Scrollspy options can be through data attributes or via JavaScript code. We need to append the option name with 'data-bs-'
3 min read
Bootstrap | Scrollspy
Sometimes while designing the website, we include some attractive features which make the website eye-catching. One of the features is Bootstrap Scrollspy which targets the navigation bar contents automatically on scrolling the area. Example 1: Scrollspy Vertical Navigation bar C/C++ Code &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;h
4 min read
How to use Scrollspy in Bootstrap 5 via JavaScript ?
In this article, we will see how to use Scrollspy in Bootstrap 5 via Javascript. Using Scrollspy in Bootstrap 5, the navigation or list group components of Bootstrap update automatically based on where the user is scrolling to indicate which link is currently active in the viewport. There are 2 ways to use Scrollspy in Bootstrap 5: Via data attribu
6 min read
Bootstrap 4 | Scrollspy
Sometimes while designing the website, we include some attractive features which makes website eye-catching. One of the features is Bootstrap scrollspy which target the navigation bar contents automatically on scrolling the area.Create scrollspy: The data-spy="scroll" and data-target=".navbar" attribute is used to create scrollspy element. The scro
3 min read
How to set the offset property for ScrollSpy in Bootstrap ?
Offset is a Bootstrap Scrollspy property wherein the user can specify the pixels to offset from top when calculating the position of scroll. It becomes useful when the user feels that either the navbar or the list changes the state early or when not needed while surfing. It is always a number and the default value is 10. The value can be changed th
8 min read
What is a scrollspy in Bootstrap ?
We have visited many websites where the navigation bars are automatically updated as the user scrolls down the webpage. Scrollspy works according to the scroll position or the position at which the user is currently seeing. Bootstrap scrollspy targets the navigation bar contents automatically on scrolling the area. In this article, we will see how
4 min read
Bootstrap 5 Scrollspy with nested nav
Bootstrap 5 Scrollspy is one of the features which targets the navigation bar contents automatically on scrolling the area. The navigation highlights the navbar links as the user scrolls the page. Only nav or list groups may be utilized with Scrollspy. Scrollspy can work with a simple navbar and also a nested navbar. A nested navbar is one in which
5 min read
Bootstrap 5 Scrollspy in Navbar
Bootstrap 5 Scrollspy in Navbar is used to apply the Scrollspy on the Navbar. If you want to shift your active navbar item throughout the scrolling then this will be helpful. Things to remember to create Scrollspy in Navbar: It is used on the navbar component.The position of the scrollspy must be relative. Anchor tags are required and must point to
4 min read
Bootstrap 5 Scrollspy Via data Attributes
Bootstrap 5 Scrollspy Via data attributes facilitate adding the scrollspy behavior in the navigation. This will only work if we add the data-bs-spy attribute to the element that is to spy, along with including the data-bs-target attribute, specifying the id or class name of the parent element of the div container or the nav component. Bootstrap 5 S
6 min read
Bootstrap 5 Scrollspy dispose() Method
Bootstrap 5 Scrollspy dispose() method is used to remove the ScrollSpy functionality from an element. To use the dispose() method, you will need to select the element that the scrollspy is applied to and call the method on it. For example, if you have applied the scrollspy to a navbar with an ID of "navbar", you can call the dispose() method as fol
4 min read