You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) is a great API.
12
-
But it may not be the one-size-fits-all solution for highlighting menu/sidebar links. You might noticed that:
12
+
But it may not be the one-size-fits-all solution to highlight menu/sidebar links.
13
13
14
-
- Scrolling speed affects accuracy
15
-
- Clicking on some links highlights different targets (or does nothing).
16
-
- Some targets never intersects once reached the bottom
17
-
- On page load, active target doesn't match the one in the URL hash.
18
-
- Is tricky to customize behavior according to different interactions
14
+
You may noticed that clicking on some links highlights the wrong one (or does nothing) and that the active link may not reflect the one in the URL hash. But most important, you noticed that's tricky to obtain different behaviors according to different scroll interactions.
19
15
20
-
<br />
16
+
For example, you want to immediately highlight targets when scroll is originated from click but not when scroll is originated from wheel/touch.
21
17
22
-
**Vue Use Active Scroll** implements a custom scroll observer that automatically deals with all these drabacks and simply returns the correct active target.
18
+
**Vue Use Active Scroll** implements a custom scroll observer which automatically adapts to different interactions and always returns the "correct" active target.
23
19
24
20
### Features
25
21
26
-
- Precise and stable at any scroll speed
27
-
- Customizable boundary offsets for each scroll direction
22
+
- Precise and stable at any speed
23
+
- CSS scroll-behavior and callback agnostic
24
+
- Adaptive behavior on mount, scroll, click, cancel.
25
+
- Customizable boundary offsets for each direction
28
26
- Customizable behavior on top/bottom reached
29
27
- Supports containers different than window
30
-
- Adaptive behavior on mount, scroll, click, cancel.
In your menu/sidebar component, provide the IDs of the targets to observe to `useActive` (order is not important):
53
-
54
-
> :bulb: For a TOC, you most likely want to target (and scroll) the headings of your content (instead of the whole section) to ensure results coherent with users' reading flow.
> :bulb: For a TOC, you want to target (and scroll) the headings of your sections (instead of the whole section) to ensure results better-aligned with users' reading flow.
Nuxt Content automatically applies IDs to your headings. You can get the TOC links by accessing `data.body.toc.links`using[queryContent](https://content.nuxtjs.org/api/composables/query-pages/).
97
+
Nuxt Content automatically applies IDs to your headings. You can get the TOC links by accessing `data.body.toc.links`via[queryContent](https://content.nuxtjs.org/api/composables/query-pages/).
154
98
155
99
```js
156
100
const { data } =awaituseAsyncData('about', () =>queryContent('/about').findOne())
| jumpToFirst |`boolean`| true | Whether to set the first target on mount as active even if not (yet) intersecting. |
209
-
| jumpToLast |`boolean`| true | Whether to set the last target as active once reached the bottom. |
153
+
| jumpToLast |`boolean`| true | Whether to set the last target as active once reached the bottom even if previous targets are entirely visible.|
210
154
| boundaryOffset |`BoundaryOffset`| { toTop: 0, toBottom: 0 } | Boundary offset in px for each scroll direction. Tweak them to "anticipate" or "delay" target detection. |
211
155
| rootId |`string`\|`null`| null | Id of the scrolling element. Set it only if your content **is not scrolled** by the window. |
212
156
| replaceHash |`boolean`| false | Whether to replace URL hash on scroll. First target is ignored if `jumpToFirst` is true. |
You are totally free to create your own click handler and choose the scrolling strategy: CSS (smooth or auto), [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) or even a scroll library like [animated-scroll-to](https://github.com/Stanko/animated-scroll-to) with custom easings will work. Just remember to include `setActive` in your handler.
202
+
Feel free to create your own click handler and to choose the scrolling strategy: CSS (smooth or auto), [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) or even a library like [animated-scroll-to](https://github.com/Stanko/animated-scroll-to) with custom easings will work. Just remember to include `setActive` in your handler.
0 commit comments