YouTip LogoYouTip

Bootstrap Scrollspy Plugin

The Scrollspy plugin automatically updates navigation links based on the scroll position. Its basic implementation adds the **.active** class to the corresponding navigation link as you scroll, based on the scroll position. If you want to use the Scrollspy plugin functionality separately, you need to include **scrollspy.js**. Alternatively, as mentioned in the (#) chapter, you can include _bootstrap.js_ or the minified version _bootstrap.min.js_. ## Usage You can add Scrollspy behavior to a top navigation: * **Via data attributes**: Add **data-spy="scroll"** to the element you want to spy on (usually the body). Then add the **data-target** attribute with the ID or class of the parent element containing the Bootstrap **.nav** components. For it to work properly, you must ensure that the body contains elements with IDs that match the links you want to spy on. ... ... * **Via JavaScript**: You can call the Scrollspy via JavaScript, selecting the element to spy on, and then calling the **.scrollspy()** function: $('body').scrollspy({ target: '.navbar-example' }) ### Example The following example demonstrates the use of the Scrollspy plugin via data attributes: ## Example

iOS

iOS is a mobile operating system developed and released by Apple. It was first released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, with which it shares the Darwin foundation. OS X is used on Apple computers, while iOS is Apple's mobile version.

SVN

Apache Subversion, commonly abbreviated as SVN, is an open-source version control system software. Subversion was created by CollabNet in 2000. However, it has now become a project of the Apache Software Foundation, and thus has a rich developer and user community.

jMeter

jMeter is an open-source testing software. It is a 100% pure Java application used for load and performance testing.

EJB

Enterprise Java Beans (EJB) is a development architecture for creating highly scalable and robust enterprise-level applications, deployed on J2EE-compatible application servers (such as JBOSS, Web Logic, etc.).

Spring

The Spring Framework is an open-source Java platform that provides comprehensive infrastructure support for rapidly developing powerful Java applications.

The Spring Framework was originally written by Rod Johnson and first released in June 2003 under the Apache 2.0 license.

[Try it Β»](#) The result is as shown below: ![Image 1: Scrollspy Plugin](#) ## Options Options can be passed via data attributes or JavaScript. The following table lists these options: | Option Name | Type/Default | Data Attribute Name | Description | | --- | --- | --- | --- | | offset | number _Default: 10_ | data-offset | Pixels to offset from top when calculating position of scroll. | ## Methods **.scrollspy('refresh')**: When calling the scrollspy method via JavaScript, you need to call the **.refresh** method to update the DOM. This is useful when any elements in the DOM have changed (i.e., you added or removed some elements). The syntax for using this method is as follows. $('').each(function () { var $spy = $(this).scrollspy('refresh') }) ### Example The following example demonstrates the usage of the **.scrollspy('refresh')** method: ## Example

iOS× Remove this section

iOS is a mobile operating system developed and released by Apple. It was first released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, with which it shares the Darwin foundation. OS X is used on Apple computers, while iOS is Apple's mobile version.

SVN

Apache Subversion, commonly abbreviated as SVN, is an open-source version control system software. Subversion was created by CollabNet in 2000. However, it has now become a project of the Apache Software Foundation, and thus has a rich developer and user community.

jMeter× Remove this section

jMeter is an open-source testing software. It is a 100% pure Java application used for load and performance testing.

EJB

Enterprise Java Beans (EJB) is a development architecture for creating highly scalable and robust enterprise-level applications, deployed on J2EE-compatible application servers (such as JBOSS, Web Logic, etc.).

Spring

The Spring Framework is an open-source Java platform that provides comprehensive infrastructure support for rapidly developing powerful Java applications.

The Spring Framework was originally written by Rod Johnson and first released in June 2003 under the Apache 2.0 license.

$(function(){ removeSection = function(e) { $(e).parents(".section").remove(); $('').each(function () { var $spy = $(this).scrollspy('refresh') }); } $("#myScrollspy").scrollspy(); }); [Try it Β»](#) The result is as shown below: ![Image 2: Scrollspy Plugin Method](#) ## Events The following table lists the events used in Scrollspy. These events can be used as hooks in functions. | Event | Description | Example | | --- | --- | --- | | activate.bs.scrollspy | This event fires whenever a new item becomes activated by the scrollspy. | $('#myScrollspy').on('activate.bs.scrollspy', function () { // do something... }) | ### Example The following example demonstrates the usage of the **activate.bs.scrollspy** event: ## Example

iOS× Remove this section

iOS is a mobile operating system developed and released by Apple. It was first released in 2007 for the iPhone, iPod Touch, and Apple TV. iOS is derived from OS X, with which it shares the Darwin foundation. OS X is used on Apple computers, while iOS is Apple's mobile version.

SVN

Apache Subversion, commonly abbreviated as SVN, is an open-source version control system software. Subversion was created by CollabNet in 2000. However, it has now become a project of the Apache Software Foundation, and thus has a rich developer and user community.

jMeter× Remove this section

jMeter is an open-source testing software. It is a 100% pure Java application used for load and performance testing.

EJB

Enterprise Java Beans (EJB) is a development architecture for creating highly scalable and robust enterprise-level applications, deployed on J2EE-compatible application servers (such as JBOSS, Web Logic, etc.).

Spring

The Spring Framework is an open-source Java platform that provides comprehensive infrastructure support for rapidly developing powerful Java applications.

The Spring Framework was originally written by Rod Johnson and first released in June 2003 under the Apache 2.0 license.

$(function(){ removeSection = function(e) { $(e).parents(".section").remove(); $('').each(function () { var $spy = $(this).scrollspy('refresh') }); } $("#myScrollspy").scrollspy(); $('#myScrollspy').on('activate.bs.scrollspy', function () { var currentItem = $(".nav li.active > a").text(); $("#activeitem").html("Currently you are viewing - " + currentItem); }) }); [Try it Β»](#) The result is as shown below: ![Image 3: Scrollspy Plugin Event](#) * * * ## More Examples ### Creating a Horizontal Scrollspy The following example demonstrates how to create a horizontal Scrollspy: ## Example

Section 1

Try to scroll this page and look at the navigation bar while scrolling!

...
[Try it Β»](#) ### How to Create a Vertical Scrollspy The following example demonstrates how to create a vertical Scrollspy: ## Example

Section 1

Try to scroll this page and look at the navigation list while scrolling!

...
[Try it Β»](#)
← Bootstrap Tab PluginBootstrap Dropdown Plugin β†’