]> git.ipfire.org Git - ipfire.org.git/blame - src/scss/bootstrap-4.0.0-alpha.6/docs/components/scrollspy.md
Introduce autotools
[ipfire.org.git] / src / scss / bootstrap-4.0.0-alpha.6 / docs / components / scrollspy.md
CommitLineData
91e44d91
S
1---
2layout: docs
3title: Scrollspy
4description: Documentation and examples for the scrollspy plugin with Bootstrap's navigation components.
5group: components
6---
7
8## Contents
9
10* Will be replaced with the ToC, excluding the "Contents" header
11{:toc}
12
13## Example in navbar
14
15The 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.
16
17<div class="bd-example">
18 <nav id="navbar-example2" class="navbar navbar-light bg-faded">
19 <a class="navbar-brand" href="#">Navbar</a>
20 <ul class="nav nav-pills">
21 <li class="nav-item"><a class="nav-link" href="#fat">@fat</a></li>
22 <li class="nav-item"><a class="nav-link" href="#mdo">@mdo</a></li>
23 <li class="nav-item dropdown">
24 <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
25 <div class="dropdown-menu">
26 <a class="dropdown-item" href="#one">one</a>
27 <a class="dropdown-item" href="#two">two</a>
28 <div role="separator" class="dropdown-divider"></div>
29 <a class="dropdown-item" href="#three">three</a>
30 </div>
31 </li>
32 </ul>
33 </nav>
34 <div data-spy="scroll" data-target="#navbar-example2" data-offset="0" class="scrollspy-example">
35 <h4 id="fat">@fat</h4>
36 <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
37 <h4 id="mdo">@mdo</h4>
38 <p>Veniam marfa mustache skateboard, adipisicing fugiat velit pitchfork beard. Freegan beard aliqua cupidatat mcsweeney's vero. Cupidatat four loko nisi, ea helvetica nulla carles. Tattooed cosby sweater food truck, mcsweeney's quis non freegan vinyl. Lo-fi wes anderson +1 sartorial. Carles non aesthetic exercitation quis gentrify. Brooklyn adipisicing craft beer vice keytar deserunt.</p>
39 <h4 id="one">one</h4>
40 <p>Occaecat commodo aliqua delectus. Fap craft beer deserunt skateboard ea. Lomo bicycle rights adipisicing banh mi, velit ea sunt next level locavore single-origin coffee in magna veniam. High life id vinyl, echo park consequat quis aliquip banh mi pitchfork. Vero VHS est adipisicing. Consectetur nisi DIY minim messenger bag. Cred ex in, sustainable delectus consectetur fanny pack iphone.</p>
41 <h4 id="two">two</h4>
42 <p>In incididunt echo park, officia deserunt mcsweeney's proident master cleanse thundercats sapiente veniam. Excepteur VHS elit, proident shoreditch +1 biodiesel laborum craft beer. Single-origin coffee wayfarers irure four loko, cupidatat terry richardson master cleanse. Assumenda you probably haven't heard of them art party fanny pack, tattooed nulla cardigan tempor ad. Proident wolf nesciunt sartorial keffiyeh eu banh mi sustainable. Elit wolf voluptate, lo-fi ea portland before they sold out four loko. Locavore enim nostrud mlkshk brooklyn nesciunt.</p>
43 <h4 id="three">three</h4>
44 <p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
45 <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.
46 </p>
47 </div>
48</div>
49
50
51## Usage
52
53### Requires Bootstrap nav
54
55Scrollspy currently requires the use of a [Bootstrap nav component]({{ site.baseurl }}/components/navs/) for proper highlighting of active links.
56
57### Requires relative positioning
58
59No 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>`. When scrollspying on elements other than the `<body>`, be sure to have a `height` set and `overflow-y: scroll;` applied.
60
61### Via data attributes
62
63To 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.
64
65{% highlight css %}
66body {
67 position: relative;
68}
69{% endhighlight %}
70
71{% highlight html %}
72<body data-spy="scroll" data-target="#navbar-example">
73 ...
74 <div id="navbar-example">
75 <ul class="nav nav-tabs" role="tablist">
76 ...
77 </ul>
78 </div>
79 ...
80</body>
81{% endhighlight %}
82
83### Via JavaScript
84
85After adding `position: relative;` in your CSS, call the scrollspy via JavaScript:
86
87{% highlight js %}
88$('body').scrollspy({ target: '#navbar-example' })
89{% endhighlight %}
90
91{% callout danger %}
92#### Resolvable ID targets required
93
94Navbar links must have resolvable id targets. For example, a `<a href="#home">home</a>` must correspond to something in the DOM like `<div id="home"></div>`.
95{% endcallout %}
96
97{% callout info %}
98#### Non-`:visible` target elements ignored
99
100Target elements that are not [`:visible` according to jQuery](https://api.jquery.com/visible-selector/) will be ignored and their corresponding nav items will never be highlighted.
101{% endcallout %}
102
103### Methods
104
105#### `.scrollspy('refresh')`
106
107When using scrollspy in conjunction with adding or removing of elements from the DOM, you'll need to call the refresh method like so:
108
109{% highlight js %}
110$('[data-spy="scroll"]').each(function () {
111 var $spy = $(this).scrollspy('refresh')
112})
113{% endhighlight %}
114
115
116### Options
117
118Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-offset=""`.
119
120<table class="table table-bordered table-striped table-responsive">
121 <thead>
122 <tr>
123 <th style="width: 100px;">Name</th>
124 <th style="width: 100px;">Type</th>
125 <th style="width: 50px;">Default</th>
126 <th>Description</th>
127 </tr>
128 </thead>
129 <tbody>
130 <tr>
131 <td>offset</td>
132 <td>number</td>
133 <td>10</td>
134 <td>Pixels to offset from top when calculating position of scroll.</td>
135 </tr>
136 </tbody>
137</table>
138
139### Events
140
141<table class="table table-bordered table-striped table-responsive">
142 <thead>
143 <tr>
144 <th style="width: 150px;">Event Type</th>
145 <th>Description</th>
146 </tr>
147 </thead>
148 <tbody>
149 <tr>
150 <td>activate.bs.scrollspy</td>
151 <td>This event fires whenever a new item becomes activated by the scrollspy.</td>
152 </tr>
153 </tbody>
154</table>
155
156{% highlight js %}
157$('#myScrollspy').on('activate.bs.scrollspy', function () {
158 // do something…
159})
160{% endhighlight %}