The Scrollspy plugin is used to create is a navigation mechanism that automatically highlights the nav links based on the scroll position to indicate to the visitor where they are currently on the page. It is particularly useful when a page has a large amount of content. It is often used with the Affix plug-in.
<style> body {position:relative;} #section1, #section2, #section3, #section41, #section42 { padding-top:50px;height:500px;color: #fff; } #section1 {background-color: #1E88E5;} #section2 {background-color: #673ab7;} #section3 {background-color: #ff9800;} #section41{background-color: #00bcd4;} #section42{background-color: #009688;} </style> </html>
<body> <!-- Header with NavBar ----------------------------- -->
<nav class="navbar navbar-inverse navbar-fixed-top" >
<div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">RMCS</a> </div> <div> <div class="collapse navbar-collapse" id="myNavbar"> <ul class="nav navbar-nav"> <li><a href="#section1">HTML 5</a></li> <li><a href="#section2">CSS 3</a></li> <li><a href="#section3">JavaScript</a></li> <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown" href="#">Apps <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#section41">App: Dreamweaver</a></li> <li><a href="#section42">App: Photoshop</a></li> </ul> </li> </ul> </div> </div> </div> </nav>
</nav> <!-- Sections 1 -------------------------------- -->
<div id="section1" class="container-fluid"> <h1>HTML 5</h1> <p><strong>INSTRUCTION:</strong>Use the scroller to "see" the menu item change while scrolling.</p> </div> <!-- Sections 2 -------------------------------- --> <div id="section2" class="container-fluid"> <h1>CSS 3</h1> </div> <!-- Sections 3 -------------------------------- --> <div id="section3" class="container-fluid"> <h1>JavaScript</h1> </div> <!-- Sections 4.1 ------------------------------ --> <div id="section41" class="container-fluid"> <h1>App: Dreamweaver</h1> </div> <!-- Sections 4.2 ------------------------------ --> <div id="section42" class="container-fluid"> <h1>App: Photoshop</h1> </div>
<body data-spy="scroll" data-target=".navbar" data-offset="50">
- The data-spy="scroll" attribute is added to the tag that should be used as the scrollable area (typically, the <body> tag).
- The data-target=".navbar" attribute is used to "target" the scrollable area.
- Scrollable elements will match the ID of the links inside the navbar's list items (<div id="section1"> matches <a href="#section1">).
- The optional data-offset attribute specifies the number of pixels to "offset" from top when calculating the position of scroll.