02.06.2015 Views

Bootstrap Tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER<br />

37<br />

<strong>Bootstrap</strong> Scrollspy Plugin<br />

The Scrollspy (auto updating nav) plugin allows you to target sections of the page based on scroll position.<br />

In its basic implementation, as you scroll, you can add .active classes to the navbar based on the scroll position.<br />

If you want to include this plugin functionality individually, then you will need scrollspy.js. Else, as mentioned in<br />

the chapter <strong>Bootstrap</strong> Plugins Overview, you can include bootstrap.js or the minified bootstrap.min.js.<br />

Usage<br />

You can add scrollspy behavior to your topbar navigation:<br />

<br />

Via data attributes: add data-spy="scroll" to the element you want to spy on (typically the body). Then add<br />

attribute data-target with the ID or class of the parent element of any <strong>Bootstrap</strong> .navcomponent. For this to<br />

work, you must have elements in the body of the page that have matching IDs of the links that you are spying<br />

on.<br />

<br />

...<br />

<br />

<br />

...<br />

<br />

<br />

...<br />

<br />

<br />

Via JavaScript: You can invoke the scrollspy with JavaScript instead of using the data attributes, by selecting<br />

the element to spy on, and then invoking the .scrollspy() function:<br />

$('body').scrollspy({ target: '.navbar-example' })<br />

EXAMPLE<br />

The following example shows the use of scrollspy plugin via data attributes:<br />

<br />

<br />

<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!