From c1d5a801b4bc66e3866f815be00e11d1b20d3539 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 24 Jun 2023 14:44:36 +0200 Subject: Adding upstream version 5.3.0+dfsg. Signed-off-by: Daniel Baumann --- site/content/docs/5.3/components/scrollspy.md | 427 ++++++++++++++++++++++++++ 1 file changed, 427 insertions(+) create mode 100644 site/content/docs/5.3/components/scrollspy.md (limited to 'site/content/docs/5.3/components/scrollspy.md') diff --git a/site/content/docs/5.3/components/scrollspy.md b/site/content/docs/5.3/components/scrollspy.md new file mode 100644 index 0000000..d1abf75 --- /dev/null +++ b/site/content/docs/5.3/components/scrollspy.md @@ -0,0 +1,427 @@ +--- +layout: docs +title: Scrollspy +description: Automatically update Bootstrap navigation or list group components based on scroll position to indicate which link is currently active in the viewport. +group: components +toc: true +--- + +## How it works + +Scrollspy toggles the `.active` class on anchor (``) elements when the element with the `id` referenced by the anchor's `href` is scrolled into view. Scrollspy is best used in conjunction with a Bootstrap [nav component]({{< docsref "/components/navs-tabs" >}}) or [list group]({{< docsref "/components/list-group" >}}), but it will also work with any anchor elements in the current page. Here's how it works. + +- To start, scrollspy requires two things: a navigation, list group, or a simple set of links, plus a scrollable container. The scrollable container can be the `` or a custom element with a set `height` and `overflow-y: scroll`. + +- On the scrollable container, add `data-bs-spy="scroll"` and `data-bs-target="#navId"` where `navId` is the unique `id` of the associated navigation. If there is no focusable element inside the element, be sure to also include a `tabindex="0"` to ensure keyboard access. + +- 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 `home` must correspond to something in the DOM like `
` + +- Target elements that are not visible will be ignored. See the [Non-visible elements](#non-visible-elements) section below. + +## Examples + +### Navbar + +Scroll the area below the navbar and watch the active class change. Open the dropdown menu and watch the dropdown items be highlighted as well. + +
+ +
+

First heading

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Second heading

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Third heading

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Fourth heading

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Fifth heading

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+
+
+ +```html + +
+

First heading

+

...

+

Second heading

+

...

+

Third heading

+

...

+

Fourth heading

+

...

+

Fifth heading

+

...

+
+``` + +### Nested nav + +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. + +
+
+
+ +
+
+
+
+

Item 1

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+
Item 1-1
+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+
Item 1-2
+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+

Item 2

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+

Item 3

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+
Item 3-1
+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+
Item 3-2
+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Keep in mind that the JavaScript plugin tries to pick the right element among all that may be visible. Multiple visible scrollspy targets at the same time may cause some issues.

+
+
+
+
+
+ +```html +
+
+ +
+ +
+
+
+

Item 1

+

...

+
+
+
Item 1-1
+

...

+
+
+
Item 1-2
+

...

+
+
+

Item 2

+

...

+
+
+

Item 3

+

...

+
+
+
Item 3-1
+

...

+
+
+
Item 3-2
+

...

+
+
+
+
+``` + +### List group + +Scrollspy also works with `.list-group`s. Scroll the area next to the list group and watch the active class change. + +
+
+
+ +
+
+
+

Item 1

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 2

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 3

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 4

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+
+
+
+
+ +```html +
+
+ +
+
+
+

Item 1

+

...

+

Item 2

+

...

+

Item 3

+

...

+

Item 4

+

...

+
+
+
+``` + +### Simple anchors + +Scrollspy is not limited to nav components and list groups, so it will work on any `` anchor elements in the current document. Scroll the area and watch the `.active` class change. + +
+
+ +
+
+

Item 1

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 2

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 3

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 4

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+

Item 5

+

This is some placeholder content for the scrollspy page. Note that as you scroll down the page, the appropriate navigation link is highlighted. It's repeated throughout the component example. We keep adding some more example copy here to emphasize the scrolling and highlighting.

+
+
+
+
+ +```html +
+
+ +
+
+
+

Item 1

+

...

+

Item 2

+

...

+

Item 3

+

...

+

Item 4

+

...

+

Item 5

+

...

+
+
+
+``` + +## Non-visible elements + +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. + +```js +document.querySelectorAll('#nav-tab>[data-bs-toggle="tab"]').forEach(el => { + el.addEventListener('shown.bs.tab', () => { + const target = el.getAttribute('data-bs-target') + const scrollElem = document.querySelector(`${target} [data-bs-spy="scroll"]`) + bootstrap.ScrollSpy.getOrCreateInstance(scrollElem).refresh() + }) +}) +``` + +## Usage + +### Via data attributes + +To easily add scrollspy behavior to your topbar navigation, add `data-bs-spy="scroll"` to the element you want to spy on (most typically this would be the ``). Then add the `data-bs-target` attribute with the `id` or class name of the parent element of any Bootstrap `.nav` component. + +```html + + ... + + ... + +``` + +### Via JavaScript + +```js +const scrollSpy = new bootstrap.ScrollSpy(document.body, { + target: '#navbar-example' +}) +``` + +### Options + +{{< markdown >}} +{{< partial "js-data-attributes.md" >}} +{{< /markdown >}} + +{{< bs-table "table" >}} +| Name | Type | Default | Description | +| --- | --- | --- | --- | +| `rootMargin` | string | `0px 0px -25%` | Intersection Observer [rootMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/rootMargin) valid units, when calculating scroll position. | +| `smoothScroll` | boolean | `false` | Enables smooth scrolling when a user clicks on a link that refers to ScrollSpy observables. | +| `target` | string, DOM element | `null` | Specifies element to apply Scrollspy plugin. | +| `threshold` | array | `[0.1, 0.5, 1]` | `IntersectionObserver` [threshold](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#threshold) valid input, when calculating scroll position. | + +{{< /bs-table >}} + +{{< callout warning >}} +**Deprecated Options** + +Up until v5.1.3 we were using `offset` & `method` options, which are now deprecated and replaced by `rootMargin`. +To keep backwards compatibility, we will continue to parse a given `offset` to `rootMargin`, but this feature will be removed in **v6**. +{{< /callout >}} + +### Methods + +{{< bs-table "table" >}} +| Method | Description | +| --- | --- | +| `dispose` | Destroys an element's scrollspy. (Removes stored data on the DOM element) | +| `getInstance` | *Static* method to get the scrollspy instance associated with a DOM element. | +| `getOrCreateInstance` | *Static* method to get the scrollspy instance associated with a DOM element, or to create a new one in case it wasn't initialized. | +| `refresh` | When adding or removing elements in the DOM, you'll need to call the refresh method. | +{{< /bs-table >}} + +Here's an example using the refresh method: + +```js +const dataSpyList = document.querySelectorAll('[data-bs-spy="scroll"]') +dataSpyList.forEach(dataSpyEl => { + bootstrap.ScrollSpy.getInstance(dataSpyEl).refresh() +}) +``` + +### Events + +{{< bs-table "table" >}} +| Event | Description | +| --- | --- | +| `activate.bs.scrollspy` | This event fires on the scroll element whenever an anchor is activated by the scrollspy. | +{{< /bs-table >}} + +```js +const firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]') +firstScrollSpyEl.addEventListener('activate.bs.scrollspy', () => { + // do something... +}) +``` -- cgit v1.2.3