Commit 537cd1c1 authored by Mark Otto's avatar Mark Otto

markdownify scrollspy

parent b1dc6384
...@@ -3,12 +3,11 @@ layout: page ...@@ -3,12 +3,11 @@ layout: page
title: Srollspy title: Srollspy
--- ---
<div class="bs-docs-section"> ## Example in navbar
<h1 id="scrollspy" class="page-header">ScrollSpy <small>scrollspy.js</small></h1>
<h2 id="scrollspy-examples">Example in navbar</h2> The ScrollSpy plugin is for automatically updating nav targets based on scroll position. Scroll the area below the navbar and watch the active class change. The dropdown sub items will be highlighted as well.
<p>The ScrollSpy plugin is for automatically updating nav targets based on scroll position. Scroll the area below the navbar and watch the active class change. The dropdown sub items will be highlighted as well.</p>
<div class="bs-example"> <div class="bs-example">
<nav id="navbar-example2" class="navbar navbar-default navbar-static" role="navigation"> <nav id="navbar-example2" class="navbar navbar-default navbar-static" role="navigation">
<div class="container-fluid"> <div class="container-fluid">
<div class="navbar-header"> <div class="navbar-header">
...@@ -51,21 +50,25 @@ title: Srollspy ...@@ -51,21 +50,25 @@ title: Srollspy
<p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats. <p>Keytar twee blog, culpa messenger bag marfa whatever delectus food truck. Sapiente synth id assumenda. Locavore sed helvetica cliche irony, thundercats you probably haven't heard of them consequat hoodie gluten-free lo-fi fap aliquip. Labore elit placeat before they sold out, terry richardson proident brunch nesciunt quis cosby sweater pariatur keffiyeh ut helvetica artisan. Cardigan craft beer seitan readymade velit. VHS chambray laboris tempor veniam. Anim mollit minim commodo ullamco thundercats.
</p> </p>
</div> </div>
</div><!-- /example --> </div>
## Usage
<h2 id="scrollspy-usage">Usage</h2> ### Requires relative positioning
<h3>Requires relative positioning</h3> No matter the implementation method, scrollspy requires the use of `position: relative;` on the element you're spying on. In most cases this is the `<body>`.
<p>No matter the implementation method, scrollspy requires the use of <code>position: relative;</code> on the element you're spying on. In most cases this is the <code>&lt;body&gt;</code>.</p>
### Via data attributes
To easily add scrollspy behavior to your topbar navigation, add `data-spy="scroll"` to the element you want to spy on (most typically this would be the `<body>`). Then add the `data-target` attribute with the ID or class of the parent element of any Bootstrap `.nav` component.
<h3>Via data attributes</h3>
<p>To easily add scrollspy behavior to your topbar navigation, add <code>data-spy="scroll"</code> to the element you want to spy on (most typically this would be the <code>&lt;body&gt;</code>). Then add the <code>data-target</code> attribute with the ID or class of the parent element of any Bootstrap <code>.nav</code> component.</p>
{% highlight css %} {% highlight css %}
body { body {
position: relative; position: relative;
} }
{% endhighlight %} {% endhighlight %}
{% highlight html %} {% highlight html %}
<body data-spy="scroll" data-target=".navbar-example"> <body data-spy="scroll" data-target=".navbar-example">
... ...
...@@ -78,24 +81,30 @@ body { ...@@ -78,24 +81,30 @@ body {
</body> </body>
{% endhighlight %} {% endhighlight %}
<h3>Via JavaScript</h3> ### Via JavaScript
<p>After adding <code>position: relative;</code> in your CSS, call the scrollspy via JavaScript:</p>
After adding `position: relative;` in your CSS, call the scrollspy via JavaScript:
{% highlight js %} {% highlight js %}
$('body').scrollspy({ target: '.navbar-example' }) $('body').scrollspy({ target: '.navbar-example' })
{% endhighlight %} {% endhighlight %}
<div class="bs-callout bs-callout-danger"> <div class="bs-callout bs-callout-danger">
<h4>Resolvable ID targets required</h4> <h4>Resolvable ID targets required</h4>
<p>Navbar links must have resolvable id targets. For example, a <code>&lt;a href="#home"&gt;home&lt;/a&gt;</code> must correspond to something in the DOM like <code>&lt;div id="home"&gt;&lt;/div&gt;</code>.</p> <p>Navbar links must have resolvable id targets. For example, a <code>&lt;a href="#home"&gt;home&lt;/a&gt;</code> must correspond to something in the DOM like <code>&lt;div id="home"&gt;&lt;/div&gt;</code>.</p>
</div> </div>
<div class="bs-callout bs-callout-info">
<div class="bs-callout bs-callout-info">
<h4>Non-<code>:visible</code> target elements ignored</h4> <h4>Non-<code>:visible</code> target elements ignored</h4>
<p>Target elements that are not <a href="http://api.jquery.com/visible-selector/"><code>:visible</code> according to jQuery</a> will be ignored and their corresponding nav items will never be highlighted.</p> <p>Target elements that are not <a href="http://api.jquery.com/visible-selector/"><code>:visible</code> according to jQuery</a> will be ignored and their corresponding nav items will never be highlighted.</p>
</div> </div>
### Methods
#### .scrollspy('refresh')
When using scrollspy in conjunction with adding or removing of elements from the DOM, you'll need to call the refresh method like so:
<h3>Methods</h3>
<h4>.scrollspy('refresh')</h4>
<p>When using scrollspy in conjunction with adding or removing of elements from the DOM, you'll need to call the refresh method like so:</p>
{% highlight js %} {% highlight js %}
$('[data-spy="scroll"]').each(function () { $('[data-spy="scroll"]').each(function () {
var $spy = $(this).scrollspy('refresh') var $spy = $(this).scrollspy('refresh')
...@@ -103,9 +112,11 @@ $('[data-spy="scroll"]').each(function () { ...@@ -103,9 +112,11 @@ $('[data-spy="scroll"]').each(function () {
{% endhighlight %} {% endhighlight %}
<h3>Options</h3> ### Options
<p>Options can be passed via data attributes or JavaScript. For data attributes, append the option name to <code>data-</code>, as in <code>data-offset=""</code>.</p>
<div class="table-responsive"> Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-offset=""`.
<div class="table-responsive">
<table class="table table-bordered table-striped"> <table class="table table-bordered table-striped">
<thead> <thead>
<tr> <tr>
...@@ -124,10 +135,11 @@ $('[data-spy="scroll"]').each(function () { ...@@ -124,10 +135,11 @@ $('[data-spy="scroll"]').each(function () {
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div><!-- ./bs-table-responsive --> </div>
### Events
<h3>Events</h3> <div class="table-responsive">
<div class="table-responsive">
<table class="table table-bordered table-striped"> <table class="table table-bordered table-striped">
<thead> <thead>
<tr> <tr>
...@@ -142,10 +154,9 @@ $('[data-spy="scroll"]').each(function () { ...@@ -142,10 +154,9 @@ $('[data-spy="scroll"]').each(function () {
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div><!-- ./bs-table-responsive --> </div>
{% highlight js %} {% highlight js %}
$('#myScrollspy').on('activate.bs.scrollspy', function () { $('#myScrollspy').on('activate.bs.scrollspy', function () {
// do something… // do something…
}) })
{% endhighlight %} {% endhighlight %}
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment