Skip to content

Commit 62b3d3c

Browse files
GeoSotjulien-deramondmdo
authoredJul 11, 2022
Add some details for non visible scrollspy elements (#36625)
* docs: add some details for non visible scrollspy elements * Apply suggestions from code review Co-authored-by: Julien Déramond <julien.deramond@orange.com> * fix selectors on snippet * Fix paragraph by a native speaker Co-authored-by: Mark Otto <markd.otto@gmail.com> * Update scrollspy.md * Move from warning to own section * Update scrollspy.md Co-authored-by: Julien Déramond <julien.deramond@orange.com> Co-authored-by: Julien Déramond <juderamond@gmail.com> Co-authored-by: Mark Otto <markd.otto@gmail.com> Co-authored-by: Mark Otto <markdotto@gmail.com>
1 parent a122448 commit 62b3d3c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed
 

‎site/content/docs/5.2/components/scrollspy.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ Scrollspy toggles the `.active` class on anchor (`<a>`) elements when the elemen
1616

1717
- As you scroll the "spied" container, an `.active` class is added and removed from anchor links within the associated navigation. Links must have resolvable `id` targets, otherwise they're ignored. For example, a `<a href="#home">home</a>` must correspond to something in the DOM like `<div id="home"></div>`
1818

19-
- Target elements that aren't visible will be ignored and their corresponding nav items will never receive an `.active` class.
20-
21-
Checkout the examples below to see it in action.
19+
- Target elements that are not visible will be ignored. See the [Non-visible elements](#non-visible-elements) section below.
2220

2321
## Examples
2422

@@ -330,6 +328,20 @@ Scrollspy is not limited to nav components and list groups, so it will work on a
330328
</div>
331329
```
332330

331+
## Non-visible elements
332+
333+
Target elements that aren’t visible will be ignored and their corresponding nav items won't receive an `.active` class. Scrollspy instances initialized in a non-visible wrapper will ignore all target elements. Use the `refresh` method to check for observable elements once the wrapper becomes visible.
334+
335+
```js
336+
document.querySelectorAll('#nav-tab>[data-bs-toggle="tab"]').forEach(el => {
337+
el.addEventListener('shown.bs.tab', () => {
338+
const target = el.getAttribute('data-bs-target')
339+
const scrollElem = document.querySelector(`${target} [data-bs-spy="scroll"]`)
340+
bootstrap.ScrollSpy.getOrCreateInstance(scrollElem).refresh()
341+
})
342+
})
343+
```
344+
333345
## Usage
334346

335347
### Via data attributes

0 commit comments

Comments
 (0)
Please sign in to comment.