Bs Scrollspy
Bootstrap Scrollspy Plugin (Advanced)
The Scrollspy Plugin
The Scrollspy plugin is used to automatically update links inwards a navigation listing based on scroll position.How To Create a Scrollspy
The next instance shows how to practise a scrollspy:Example
<!-- The scrollable surface area -->
<body data-spy="scroll" data-target=".navbar" data-offset="50">
<!-- The navbar - The <a> elements are used to boundary to a department inwards the scrollable surface area -->
<nav class="navbar navbar-inverse navbar-fixed-top">
...
<ul class="nav navbar-nav">
<li><a href="#section1">Section 1</a></li>
...
</nav>
<!-- Section i -->
<div id="section1">
<h1>Section 1</h1>
<p>Try to scroll this page together with expect at the navigation bar spell scrolling!</p>
</div>
...
</body>
Example Explained
Adddata-spy="scroll"
to the chemical constituent that should hold upwardly used equally the scrollable surface area (often this is the <body>
element).Then add together the
data-target
attribute alongside a value of the id or the aeroplane refer of the navigation bar (.navbar
). This is to brand certain that the navbar is connected alongside the scrollable area.Note that scrollable elements must gibe the ID of the links within the navbar's listing items (
<div id="section1">
matches <a href="#section1">
).The optional
data-offset
attribute specifies the reveal of pixels to outset from altitude when calculating the rate of scroll. This is useful when you lot experience that the links within the navbar changes the active state equally good before long or equally good early on when jumping to the scrollable elements. Default is 10 pixels. Requires relative positioning: The chemical constituent alongside data-spy="scroll" requires the CSS position property, alongside a value of "relative" to run properly.
Scrollspy Vertical Menu
In this example, nosotros role Bootstrap's vertical navigation pills equally menu: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 together with expect at the navigation listing spell scrolling!</p>
</div>
...
</div>
</div>
</div>
</body>
Comments
Post a Comment