Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: Scrollspy
4
description: Automatically update Bootstrap navigation or list group components based on scroll position to indicate which link is currently active in the viewport.
5
group: components
6
toc: true
7
---
8
 
9
## How it works
10
 
11
Scrollspy has a few requirements to function properly:
12
 
13
- If you're building our JavaScript from source, it [requires `util.js`]({{< docsref "/getting-started/javascript#util" >}}).
14
- It must be used on a Bootstrap [nav component]({{< docsref "/components/navs" >}}) or [list group]({{< docsref "/components/list-group" >}}).
15
- Scrollspy requires `position: relative;` on the element you're spying on, usually the `<body>`.
16
- When spying on elements other than the `<body>`, be sure to have a `height` set and `overflow-y: scroll;` applied.
17
- Anchors (`<a>`) are required and must point to an element with that `id`.
18
 
19
When successfully implemented, your nav or list group will update accordingly, moving the `.active` class from one item to the next based on their associated targets.
20
 
21
## Example in navbar
22
 
23
Scroll the area below the navbar and watch the active class change. The dropdown items will be highlighted as well.
24
 
25
<div class="bd-example">
26
  <nav id="navbar-example2" class="navbar navbar-light bg-light">
27
    <a class="navbar-brand" href="#">Navbar</a>
28
    <ul class="nav nav-pills">
29
      <li class="nav-item">
30
        <a class="nav-link" href="#fat">@fat</a>
31
      </li>
32
      <li class="nav-item">
33
        <a class="nav-link" href="#mdo">@mdo</a>
34
      </li>
35
      <li class="nav-item dropdown">
36
        <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
37
        <div class="dropdown-menu">
38
          <a class="dropdown-item" href="#one">one</a>
39
          <a class="dropdown-item" href="#two">two</a>
40
          <div role="separator" class="dropdown-divider"></div>
41
          <a class="dropdown-item" href="#three">three</a>
42
        </div>
43
      </li>
44
    </ul>
45
  </nav>
46
  <div data-spy="scroll" data-target="#navbar-example2" data-offset="0" class="scrollspy-example">
47
    <h4 id="fat">@fat</h4>
48
    <p>Placeholder content for the scrollspy example. You got the finest architecture. Passport stamps, she's cosmopolitan. Fine, fresh, fierce, we got it on lock. Never planned that one day I'd be losing you. She eats your heart out. Your kiss is cosmic, every move is magic. I mean the ones, I mean like she's the one. Greetings loved ones let's take a journey. Just own the night like the 4th of July! But you'd rather get wasted.</p>
49
    <h4 id="mdo">@mdo</h4>
50
    <p>Placeholder content for the scrollspy example. 'Cause she's the muse and the artist. (This is how we do) So you wanna play with magic. So just be sure before you give it all to me. I'm walking, I'm walking on air (tonight). Skip the talk, heard it all, time to walk the walk.</p>
51
    <h4 id="one">one</h4>
52
    <p>Placeholder content for the scrollspy example. Takes you miles high, so high, 'cause she’s got that one international smile. There's a stranger in my bed, there's a pounding in my head. Oh, no. In another life I would make you stay. ‘Cause I, I’m capable of anything. Suiting up for my crowning battle. Used to steal your parents' liquor and climb to the roof. Tone, tan fit and ready, turn it up cause its gettin' heavy. Her love is like a drug. I guess that I forgot I had a choice.</p>
53
    <h4 id="two">two</h4>
54
    <p>Placeholder content for the scrollspy example. It's time to bring out the big balloons. I'm walking, I'm walking on air (tonight). Yeah, we maxed our credit cards and got kicked out of the bar. Yo, shout out to all you kids, buying bottle service, with your rent money. I'm ma get your heart racing in my skin-tight jeans. If you get the chance you better keep her. Yo, shout out to all you kids, buying bottle service, with your rent money.</p>
55
    <h4 id="three">three</h4>
56
    <p>Placeholder content for the scrollspy example. If you wanna dance, if you want it all, you know that I'm the girl that you should call. Walk through the storm I would. So let me get you in your birthday suit. The one that got away. Last Friday night, yeah I think we broke the law, always say we're gonna stop. 'Cause she's a little bit of Yoko, And she's a little bit of 'Oh no'. I want the jaw droppin', eye poppin', head turnin', body shockin'. Yeah, we maxed our credit cards and got kicked out of the bar.</p>
57
    <p>And some more placeholder content, for good measure.</p>
58
  </div>
59
</div>
60
 
61
```html
62
<nav id="navbar-example2" class="navbar navbar-light bg-light">
63
  <a class="navbar-brand" href="#">Navbar</a>
64
  <ul class="nav nav-pills">
65
    <li class="nav-item">
66
      <a class="nav-link" href="#fat">@fat</a>
67
    </li>
68
    <li class="nav-item">
69
      <a class="nav-link" href="#mdo">@mdo</a>
70
    </li>
71
    <li class="nav-item dropdown">
72
      <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-expanded="false">Dropdown</a>
73
      <div class="dropdown-menu">
74
        <a class="dropdown-item" href="#one">one</a>
75
        <a class="dropdown-item" href="#two">two</a>
76
        <div role="separator" class="dropdown-divider"></div>
77
        <a class="dropdown-item" href="#three">three</a>
78
      </div>
79
    </li>
80
  </ul>
81
</nav>
82
<div data-spy="scroll" data-target="#navbar-example2" data-offset="0">
83
  <h4 id="fat">@fat</h4>
84
  <p>...</p>
85
  <h4 id="mdo">@mdo</h4>
86
  <p>...</p>
87
  <h4 id="one">one</h4>
88
  <p>...</p>
89
  <h4 id="two">two</h4>
90
  <p>...</p>
91
  <h4 id="three">three</h4>
92
  <p>...</p>
93
</div>
94
```
95
 
96
## Example with nested nav
97
 
98
Scrollspy also works with nested `.nav`s. If a nested `.nav` is `.active`, its parents will also be `.active`. Scroll the area next to the navbar and watch the active class change.
99
 
100
<div class="bd-example">
101
  <div class="row">
102
    <div class="col-4">
103
      <nav id="navbar-example3" class="navbar navbar-light bg-light flex-column">
104
        <a class="navbar-brand" href="#">Navbar</a>
105
        <nav class="nav nav-pills flex-column">
106
          <a class="nav-link" href="#item-1">Item 1</a>
107
          <nav class="nav nav-pills flex-column">
108
            <a class="nav-link ml-3 my-1" href="#item-1-1">Item 1-1</a>
109
            <a class="nav-link ml-3 my-1" href="#item-1-2">Item 1-2</a>
110
          </nav>
111
          <a class="nav-link" href="#item-2">Item 2</a>
112
          <a class="nav-link" href="#item-3">Item 3</a>
113
          <nav class="nav nav-pills flex-column">
114
            <a class="nav-link ml-3 my-1" href="#item-3-1">Item 3-1</a>
115
            <a class="nav-link ml-3 my-1" href="#item-3-2">Item 3-2</a>
116
          </nav>
117
        </nav>
118
      </nav>
119
    </div>
120
    <div class="col-8">
121
      <div data-spy="scroll" data-target="#navbar-example3" data-offset="0" class="scrollspy-example-2">
122
        <h4 id="item-1">Item 1</h4>
123
        <p>Placeholder content for the scrollspy example. This one relates to item 1. Takes you miles high, so high, 'cause she’s got that one international smile. There's a stranger in my bed, there's a pounding in my head. Oh, no. In another life I would make you stay. ‘Cause I, I’m capable of anything. Suiting up for my crowning battle. Used to steal your parents' liquor and climb to the roof. Tone, tan fit and ready, turn it up cause its gettin' heavy. Her love is like a drug. I guess that I forgot I had a choice.</p>
124
        <h5 id="item-1-1">Item 1-1</h5>
125
        <p>Placeholder content for the scrollspy example. This one relates to the item 1-1. You got the finest architecture. Passport stamps, she's cosmopolitan. Fine, fresh, fierce, we got it on lock. Never planned that one day I'd be losing you. She eats your heart out. Your kiss is cosmic, every move is magic. I mean the ones, I mean like she's the one. Greetings loved ones let's take a journey. Just own the night like the 4th of July! But you'd rather get wasted.</p>
126
        <h5 id="item-1-2">Item 1-2</h5>
127
        <p>Placeholder content for the scrollspy example. This one relates to the item 1-2. Her love is like a drug. All my girls vintage Chanel baby. Got a motel and built a fort out of sheets. 'Cause she's the muse and the artist. (This is how we do) So you wanna play with magic. So just be sure before you give it all to me. I'm walking, I'm walking on air (tonight). Skip the talk, heard it all, time to walk the walk. Catch her if you can. Stinging like a bee I earned my stripes.</p>
128
        <h4 id="item-2">Item 2</h4>
129
        <p>Placeholder content for the scrollspy example. This one relates to item 2. Don't need apologies. There is no fear now, let go and just be free, I will love you unconditionally. Last Friday night. Don't be a shy kinda guy I'll bet it's beautiful. Summer after high school when we first met. 'Cause she's the muse and the artist. What? Wait. No, no, no, no. Thought that I was the exception.</p>
130
        <h4 id="item-3">Item 3</h4>
131
        <p>Placeholder content for the scrollspy example. This one relates to item 3. Word on the street, you got somethin' to show me, me. All this money can't buy me a time machine. Make it like your birthday everyday. So we hit the boulevard. You make me feel like I'm livin' a teenage dream, the way you turn me on Skip the talk, heard it all, time to walk the walk. Word on the street, you got somethin' to show me, me. It's no big deal, it's no big deal, it's no big deal.</p>
132
        <h5 id="item-3-1">Item 3-1</h5>
133
        <p>Placeholder content for the scrollspy example. This one relates to item 3-1. Baby do you dare to do this? This is no big deal. Yeah, you're lucky if you're on her plane. Just own the night like the 4th of July! Standing on the frontline when the bombs start to fall. So just be sure before you give it all to me.</p>
134
        <h5 id="item-3-2">Item 3-2</h5>
135
        <p>Placeholder content for the scrollspy example. This one relates to item 3-2. You're original, cannot be replaced. All night they're playing, your song. California girls we're undeniable. Like a bird without a cage. There is no fear now, let go and just be free, I will love you unconditionally. I can see the writing on the wall. You could travel the world but nothing comes close to the golden coast.</p>
136
      </div>
137
    </div>
138
  </div>
139
</div>
140
 
141
```html
142
<nav id="navbar-example3" class="navbar navbar-light bg-light">
143
  <a class="navbar-brand" href="#">Navbar</a>
144
  <nav class="nav nav-pills flex-column">
145
    <a class="nav-link" href="#item-1">Item 1</a>
146
    <nav class="nav nav-pills flex-column">
147
      <a class="nav-link ml-3 my-1" href="#item-1-1">Item 1-1</a>
148
      <a class="nav-link ml-3 my-1" href="#item-1-2">Item 1-2</a>
149
    </nav>
150
    <a class="nav-link" href="#item-2">Item 2</a>
151
    <a class="nav-link" href="#item-3">Item 3</a>
152
    <nav class="nav nav-pills flex-column">
153
      <a class="nav-link ml-3 my-1" href="#item-3-1">Item 3-1</a>
154
      <a class="nav-link ml-3 my-1" href="#item-3-2">Item 3-2</a>
155
    </nav>
156
  </nav>
157
</nav>
158
 
159
<div data-spy="scroll" data-target="#navbar-example3" data-offset="0">
160
  <h4 id="item-1">Item 1</h4>
161
  <p>...</p>
162
  <h5 id="item-1-1">Item 1-1</h5>
163
  <p>...</p>
164
  <h5 id="item-1-2">Item 1-2</h5>
165
  <p>...</p>
166
  <h4 id="item-2">Item 2</h4>
167
  <p>...</p>
168
  <h4 id="item-3">Item 3</h4>
169
  <p>...</p>
170
  <h5 id="item-3-1">Item 3-1</h5>
171
  <p>...</p>
172
  <h5 id="item-3-2">Item 3-2</h5>
173
  <p>...</p>
174
</div>
175
```
176
 
177
## Example with list-group
178
 
179
Scrollspy also works with `.list-group`s. Scroll the area next to the list group and watch the active class change.
180
 
181
<div class="bd-example">
182
  <div class="row">
183
    <div class="col-4">
184
      <div id="list-example" class="list-group">
185
        <a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
186
        <a class="list-group-item list-group-item-action" href="#list-item-2">Item 2</a>
187
        <a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
188
        <a class="list-group-item list-group-item-action" href="#list-item-4">Item 4</a>
189
      </div>
190
    </div>
191
    <div class="col-8">
192
      <div data-spy="scroll" data-target="#list-example" data-offset="0" class="scrollspy-example">
193
        <h4 id="list-item-1">Item 1</h4>
194
        <p>Placeholder content for the scrollspy list-group example. This one relates to item 1. Don't need apologies. There is no fear now, let go and just be free, I will love you unconditionally. Last Friday night. Don't be a shy kinda guy I'll bet it's beautiful. Summer after high school when we first met. 'Cause she's the muse and the artist. What? Wait. Thought that I was the exception.</p>
195
        <h4 id="list-item-2">Item 2</h4>
196
        <p>Placeholder content for the scrollspy list-group example. This one relates to item 2. Yeah, she dances to her own beat. Oh, no. You could've been the greatest. 'Cause, baby, you're a firework. Maybe a reason why all the doors are closed. Open up your heart and just let it begin. So très chic, yeah, she's a classic.</p>
197
        <h4 id="list-item-3">Item 3</h4>
198
        <p>Placeholder content for the scrollspy list-group example. This one relates to item 3. We go higher and higher. Never one without the other, we made a pact. I'm walking on air. Someone said you had your tattoo removed. Now I’m floating like a butterfly. Tone, tan fit and ready, turn it up cause its gettin' heavy. Cause once you’re mine, once you’re mine. You just gotta ignite the light and let it shine! So we hit the boulevard. Do you ever feel, feel so paper thin. I see it all, I see it now.</p>
199
        <h4 id="list-item-4">Item 4</h4>
200
        <p>Placeholder content for the scrollspy list-group example. This one relates to item 4. Summer after high school when we first met. There is no fear now, let go and just be free, I will love you unconditionally. Sun-kissed skin so hot we'll melt your popsicle. This love will make you levitate.</p>
201
      </div>
202
    </div>
203
  </div>
204
</div>
205
 
206
```html
207
<div id="list-example" class="list-group">
208
  <a class="list-group-item list-group-item-action" href="#list-item-1">Item 1</a>
209
  <a class="list-group-item list-group-item-action" href="#list-item-2">Item 2</a>
210
  <a class="list-group-item list-group-item-action" href="#list-item-3">Item 3</a>
211
  <a class="list-group-item list-group-item-action" href="#list-item-4">Item 4</a>
212
</div>
213
<div data-spy="scroll" data-target="#list-example" data-offset="0" class="scrollspy-example">
214
  <h4 id="list-item-1">Item 1</h4>
215
  <p>...</p>
216
  <h4 id="list-item-2">Item 2</h4>
217
  <p>...</p>
218
  <h4 id="list-item-3">Item 3</h4>
219
  <p>...</p>
220
  <h4 id="list-item-4">Item 4</h4>
221
  <p>...</p>
222
</div>
223
```
224
 
225
## Usage
226
 
227
### Via data attributes
228
 
229
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.
230
 
231
```css
232
body {
233
  position: relative;
234
}
235
```
236
 
237
```html
238
<body data-spy="scroll" data-target="#navbar-example">
239
  ...
240
  <div id="navbar-example">
241
    <ul class="nav nav-tabs" role="tablist">
242
      ...
243
    </ul>
244
  </div>
245
  ...
246
</body>
247
```
248
 
249
### Via JavaScript
250
 
251
After adding `position: relative;` in your CSS, call the scrollspy via JavaScript:
252
 
253
```js
254
$('body').scrollspy({ target: '#navbar-example' })
255
```
256
 
257
{{< callout danger >}}
258
#### Resolvable ID targets required
259
 
260
Navbar 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>`.
261
{{< /callout >}}
262
 
263
{{< callout info >}}
264
#### Non-`:visible` target elements ignored
265
 
266
Target 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.
267
{{< /callout >}}
268
 
269
### Methods
270
 
271
#### `.scrollspy('refresh')`
272
 
273
When using scrollspy in conjunction with adding or removing of elements from the DOM, you'll need to call the refresh method like so:
274
 
275
```js
276
$('[data-spy="scroll"]').each(function () {
277
  var $spy = $(this).scrollspy('refresh')
278
})
279
```
280
 
281
#### `.scrollspy('dispose')`
282
 
283
Destroys an element's scrollspy.
284
 
285
### Options
286
 
287
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to `data-`, as in `data-offset=""`.
288
 
289
<table class="table table-bordered table-striped">
290
  <thead>
291
    <tr>
292
      <th style="width: 100px;">Name</th>
293
      <th style="width: 100px;">Type</th>
294
      <th style="width: 50px;">Default</th>
295
      <th>Description</th>
296
    </tr>
297
  </thead>
298
  <tbody>
299
    <tr>
300
      <td>offset</td>
301
      <td>number</td>
302
      <td>10</td>
303
      <td>Pixels to offset from top when calculating position of scroll.</td>
304
    </tr>
305
    <tr>
306
      <td>method</td>
307
      <td>string</td>
308
      <td>auto</td>
309
      <td>Finds which section the spied element is in. <code>auto</code> will choose the best method to get scroll coordinates. <code>offset</code> will use jQuery offset method to get scroll coordinates. <code>position</code> will use jQuery position method to get scroll coordinates.</td>
310
    </tr>
311
    <tr>
312
      <td>target</td>
313
      <td>string | jQuery object | DOM element</td>
314
      <td></td>
315
      <td>Specifies element to apply Scrollspy plugin.</td>
316
    </tr>
317
  </tbody>
318
</table>
319
 
320
### Events
321
 
322
<table class="table table-bordered table-striped">
323
  <thead>
324
    <tr>
325
      <th style="width: 150px;">Event Type</th>
326
      <th>Description</th>
327
    </tr>
328
  </thead>
329
  <tbody>
330
    <tr>
331
      <td>activate.bs.scrollspy</td>
332
      <td>This event fires on the scroll element whenever a new item becomes activated by the scrollspy.</td>
333
    </tr>
334
  </tbody>
335
</table>
336
 
337
```js
338
$('[data-spy="scroll"]').on('activate.bs.scrollspy', function () {
339
  // do something...
340
})
341
```