Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Scrollspy (Scrollspy) Plugin

Bootstrap Scrollspy (Scrollspy) Plugin

The Scrollspy (Scrollspy) plugin, also known as the auto-updating navigation plugin, automatically updates the corresponding navigation target based on the position of the scrollbar. Its basic implementation is to add to the navbar, as you scroll, based on the scrollbar position, the .active class。

If you want to reference the plugin's functionality separately, you need to include scrollspy.js. Or, as Bootstrap Plugins Overview chapter mentions, you can include bootstrap.js or the compressed version bootstrap.min.js。

Usage

You can add scrollspy behavior to the top navigation:

- **Via JavaScript**: you can call scrollspy through JavaScript, select the element to spy on, then call the **.scrollspy()** function:
$('body').scrollspy({ target: '.navbar-example' })

Example

The following example demonstrates using the Scrollspy (Scrollspy) plugin via data attributes:

Example

<nav id="navbar-example" class=">

navbar navbar-default navbar-static

"

role

=

"

navigation

"

<div class="container-fluid">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-js-navbar-scrollspy">
<span class="sr-only">

Toggle Navigation

span

<span class="icon-bar" span>
<span class="icon-bar" span>
<span class="icon-bar" span button>
<a class="navbar-brand" href="">

Tutorial Name

a

div

<div class=">

collapse navbar-collapse bs-js-navbar-scrollspy

"

<ul class=">

nav navbar-nav

"

<li>
<a href="#ios" iOS a li>
<li>
<a href="#svn" SVN a li>
<li class="dropdown">
<a href="" id="navbarDrop1" class="dropdown-toggle" data-toggle="dropdown" Java>
<b class="caret" b a>
<ul class="dropdown-menu" role="menu" aria-labelledby="navbarDrop1">
<li>
<a href="#jmeter" tabindex="-1" jmeter a li>
<li>
<a href="#ejb" tabindex="-1" ejb a li>
<li class="divider" li>
<li>
<a href="#spring" tabindex="-1" spring a li ul li ul div div nav>
<div data-spy="scroll" data-target="#navbar-example" data-offset="0" style=">

height:200px;overflow:auto; position: relative;

"

<h4 id="ios" iOS h4>
<p>

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 derives from OS X, and they share the Darwin foundation. The OS X operating system is used on Apple computers, while iOS is Apple's mobile version.

p

<h4 id="svn" SVN h4>
<p>

Apache Subversion, commonly abbreviated as SVN, is an open-source version control system software. Subversion was created by CollabNet in 2000. But now it has developed into a project of the Apache Software Foundation, and therefore has a rich community of developers and users.

p

<h4 id="jmeter" jMeter h4>
<p>

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

p

<h4 id="ejb" EJB h4>
<p>

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

p

<h4 id="spring" Spring h4>
<p>

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

p

<

p

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

p

div

Try it »

The result is as follows:

Option

passed through data attributes or JavaScript. The following table lists these options:

Option name Type / Default value Data attribute name Description
offset number
Default value: 10 data-offset The offset in pixels from the top when calculating the scroll position.

Method

.scrollspy('refresh')

: when calling the scrollspy method through JavaScript, you need to call the

.refresh

method to update the DOM. This is very useful when any element in the DOM changes (i.e., you added or removed some elements). Below is the syntax for using this method.



$('[data-spy="scroll"]').each(function () {

  var $spy = $(this).scrollspy('refresh')

})

Example

The following example demonstrates .scrollspy('refresh') method usage:

Example

<nav id="myScrollspy" class=">

navbar navbar-default navbar-static

"

role

=

"

navigation

"

<div class="container-fluid">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-js-navbar-scrollspy">
<span class="sr-only">

Toggle Navigation

span

<span class="icon-bar" span>
<span class="icon-bar" span>
<span class="icon-bar" span button>
<a class="navbar-brand" href="">

Tutorial Name

a

div

<div class=">

collapse navbar-collapse bs-js-navbar-scrollspy

"

<ul class=">

nav navbar-nav

"

<li class="active">
<a href="#ios" iOS a li>
<li>
<a href="#svn" SVN a li>
<li class="dropdown">
<a href="" id="navbarDrop1" class="dropdown-toggle" data-toggle="dropdown" Java>
<b class="caret" b a>
<ul class="dropdown-menu" role="menu" aria-labelledby="navbarDrop1">
<li>
<a href="#jmeter" tabindex="-1" jmeter a li>
<li>
<a href="#ejb" tabindex="-1" ejb a li>
<li class="divider" li>
<li>
<a href="#spring" tabindex="-1" spring a li ul li ul div div nav>
<div data-spy="scroll" data-target="#myScrollspy" data-offset="0" style=">

height:200px;overflow:auto; position: relative;

"

<div class="section">
<h4 id="ios" iOS>
<small>
<a href="" onclick=">

removeSection(this);

"

×

Remove This Section

a

small

h4

<

p

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 derives from OS X, and they share the Darwin foundation. The OS X operating system is used on Apple computers, while iOS is Apple's mobile version.

p

div

<div class="section">
<h4 id="svn" SVN>
<small small h4>
<p>

Apache Subversion, commonly abbreviated as SVN, is an open-source version control system software. Subversion was created by CollabNet in 2000. But now it has developed into a project of the Apache Software Foundation, and therefore has a rich community of developers and users.

p

div

<div class="section">
<h4 id="jmeter" jMeter>
<small>
<a href="" onclick=">

removeSection(this);

"

×

Remove This Section

a

small

h4

<

p

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

p

div

<div class="section">
<h4 id="ejb" EJB h4>
<p>

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

p

div

<div class="section">
<h4 id="spring" Spring h4>
<p>

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

p

<

p

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

p

div

div

<

script

$(function(){ removeSection = function(e) { $(e).parents(".section").remove(); $('[data-spy="scroll"]').each(function () { var $spy = $(this).scrollspy('refresh') }); } $("#myScrollspy").scrollspy(); });

script

Try it »

The result is as follows:

Events

The following table lists the events used in scrollspy. These events can be used as hooks in functions.

Events Description Example
activate.bs.scrollspy This event is fired whenever a new item is activated by scrollspy.

$('#myScrollspy').on('activate.bs.scrollspy', function () { // do something... }) |

Example

The following example demonstrates activate.bs.scrollspy Event Usage:

Example

<nav id="myScrollspy" class=">

navbar navbar-default navbar-static

"

role

=

"

navigation

"

<div class="container-fluid">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-js-navbar-scrollspy">
<span class="sr-only">

Toggle Navigation

span

<span class="icon-bar" span>
<span class="icon-bar" span>
<span class="icon-bar" span button>
<a class="navbar-brand" href="">

Tutorial Name

a

div

<div class=">

collapse navbar-collapse bs-js-navbar-scrollspy

"

<ul class=">

nav navbar-nav

"

<li class="active">
<a href="#ios" iOS a li>
<li>
<a href="#svn" SVN a li>
<li class="dropdown">
<a href="" id="navbarDrop1" class="dropdown-toggle" data-toggle="dropdown" Java>
<b class="caret" b a>
<ul class="dropdown-menu" role="menu" aria-labelledby="navbarDrop1">
<li>
<a href="#jmeter" tabindex="-1" jmeter a li>
<li>
<a href="#ejb" tabindex="-1" ejb a li>
<li class="divider" li>
<li>
<a href="#spring" tabindex="-1" spring a li ul li ul div div nav>
<div data-spy="scroll" data-target="#myScrollspy" data-offset="0" style=">

height:200px;overflow:auto; position: relative;

"

<div class="section">
<h4 id="ios" iOS>
<small>
<a href="" onclick=">

removeSection(this);

"

×

Remove This Section

a

small

h4

<

p

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 derives from OS X, and they share the Darwin foundation. The OS X operating system is used on Apple computers, while iOS is Apple's mobile version.

p

div

<div class="section">
<h4 id="svn" SVN>
<small small h4>
<p>

Apache Subversion, commonly abbreviated as SVN, is an open-source version control system software. Subversion was created by CollabNet in 2000. But now it has developed into a project of the Apache Software Foundation, and therefore has a rich community of developers and users.

p

div

<div class="section">
<h4 id="jmeter" jMeter>
<small>
<a href="" onclick=">

removeSection(this);

"

×

Remove This Section

a

small

h4

<

p

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

p

div

<div class="section">
<h4 id="ejb" EJB h4>
<p>

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

p

div

<div class="section">
<h4 id="spring" Spring h4>
<p>

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

p

<

p

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

p

div

div

<span id="activeitem" style=">

color:red;

"

span

<

script

$(function(){ removeSection = function(e) { $(e).parents(".section").remove(); $('[data-spy="scroll"]').each(function () { var $spy = $(this).scrollspy('refresh') }); } $("#myScrollspy").scrollspy(); $('#myScrollspy').on('activate.bs.scrollspy', function () { var currentItem = $(".nav li.active > a").text(); $("#activeitem").html("You are currently viewing - " + currentItem); }) });

script

Try it »

The result is as follows:


More examples

Create a Horizontal Scrollspy

The following example demonstrates how to create a horizontal scrollspy:

Example

<

nav

class=

"navbar navbar-inverse navbar-fixed-top"

...

 

<

ul

class=

"nav navbar-nav"

   

<

li

<

a

href=

"#section1"

Section 1

<

/a

<

/li

    ...

<

/nav

<

div

data-spy=

"scroll"

data-target=

".navbar"

data-offset=

"12"

<

div

id=

"section1"

<

h1

Section 1

<

/h1

<

p

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

<

/p

<

/div

  ...

<

/div

Try it »

How to Create a Vertical Scrollspy

The following example demonstrates how to create a vertical scrollspy:

Example

<

body

data-spy=

"scroll"

data-target=

"#myScrollspy"

data-offset=

"20"

<

div

class=

"container"

<

div

class=

"row"

<

nav

class=

"col-sm-3"

id=

"myScrollspy"

<

ul

class=

"nav nav-pills nav-stacked"

         

<

li

<

a

href=

"#section1"

Section 1

<

/a

<

/li

          ...

       

<

/ul

<

/nav

<

div

class=

"col-sm-9"

<

div

id=

"section1"

<

h1

Section 1

<

/h1

<

p

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

<

/p

<

/div

        ...

     

<

/div

<

/div

<

/div

<

/body

Try it »