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.
15
13
But it may not be the one-size-fits-all solution to highlight menu/sidebar links.
16
14
17
-
When smooth-scrolling, you may want to immediately highlight targets when scroll is originated from click/navigation but not when it is originated from wheel/touch. You may also want to highlight any clicked link even if it will never intersect.
15
+
_You may want to:_
16
+
17
+
- Highlight any clicked link even if it will never intersect
18
+
- Get consistent results regardless of scroll speed
19
+
- Immediately highlight links on click/hash navigation if smooth scrolling is enabled
20
+
- Prevent unnatural highlighting with custom easings or smooth scrolling
18
21
19
-
**Vue Use Active Scroll** implements a custom scroll observer which automatically adapts to any type of scroll behaviors and interactions and always returns the "correct" active target.
22
+
**Vue Use Active Scroll** implements a custom scroll observer which automatically adapts to any type of scroll behavior and interaction and always returns the "correct" active target.
- Customizable boundary offsets for each direction
27
-
- Customizable offsets for first/last targets
29
+
- Customizable offsets for each scroll direction
30
+
- Customizable offsets for first and last target
28
31
- Customizable behavior on top/bottom reached
29
32
- Supports custom scrolling containers
30
33
31
34
### What it doesn't do?
32
35
33
-
- Mutate elements and inject styles
34
-
- Force specific scroll behavior / callbacks
35
36
- Scroll to targets
37
+
- Mutate elements and inject styles
38
+
- Enforce specific scroll behavior / callbacks
39
+
- Enforce the use of hash navigation
36
40
37
41
<br />
38
42
@@ -59,7 +63,7 @@ Assuming your content looks like:
59
63
<p>...</p>
60
64
```
61
65
62
-
And your links look like:
66
+
And your links will look like:
63
67
64
68
```html
65
69
<ahref="#introduction">Introduction</a>
@@ -70,20 +74,22 @@ And your links look like:
70
74
In your menu/sidebar component, provide the IDs to observe to `useActive` (order is not
71
75
important).
72
76
77
+
> :bulb: For a TOC, you may want to target (and scroll) the headings of your sections (instead of the whole section) to ensure results better-aligned with users' reading flow.
78
+
73
79
```vue
74
80
<!-- Sidebar.vue -->
75
81
76
82
<script setup>
77
83
import { useActive } from 'vue-use-active-scroll'
78
84
79
-
// Data to render links
85
+
// Data to render links, in real life you may pass them as prop, use inject() etc...
You can provide either a reactive or a plain array of strings. If the array is reactive, the observer will reinitialize whenever it changes.
94
100
95
-
> :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.
101
+
If an empty array is provided, the observer won't be initialized until the array is populated.
102
+
103
+
### What if my targets don't have IDs?
104
+
105
+
There might be cases where you lack control over the rendered HTML and no IDs nor TOC are provided. Assuming your content is wrapped by container with an ID (e.g. `#ArticleContent`):
### **1.** Call _setActive_ in your click handler by passing the anchor ID
182
222
223
+
> :bulb: This doesn't scroll to targets. It just informs the composable that scroll from click is about to happen and `useActive` will adjust its behavior accordingly.
You're free to choose between 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).
211
253
212
-
#### A. Using native CSS scroll-behavior
254
+
#### A. Using native CSS scroll-behavior (recommended)
213
255
214
256
- If content is scrolled by the window, add the following CSS rule to your `html` element:
active: isActive(link.href) /* 👈🏻 or link.href === activeId */
337
+
ActiveLink: isActive(link.href) /* 👈🏻 or link.href === activeId */
296
338
}"
297
339
>
298
340
{{ link.label }}
@@ -306,7 +348,7 @@ html {
306
348
scroll-behavior: smooth; /* or 'auto' */
307
349
}
308
350
309
-
.active {
351
+
.ActiveLink {
310
352
color: #f00;
311
353
}
312
354
</style>
@@ -355,7 +397,7 @@ html {
355
397
356
398
## Setting scroll-margin-top for fixed headers
357
399
358
-
You might noticed that if you have a fixed header and defined an `overlayHeight`, once you click to scroll to a target it may be underneath the header. You must add `scroll-margin-top` to your targets:
400
+
You might noticed that if you have a fixed header and defined an `overlayHeight`, once clicked to scroll, the target may be underneath the header. You must add `scroll-margin-top` to your targets:
## Vue Router - Scroll to hash onMount / navigation
414
+
## Vue Router - Scroll to hash on mount / navigation
373
415
374
416
> :warning: If using Nuxt 3, Vue Router is already configured to scroll to and from URL hash on page load or back/forward navigation. **So you don't need to do follow the steps below**. Otherwise rules must be defined manually.
375
417
@@ -449,33 +491,6 @@ If you don't like that, choose to replace instead of pushing the hash:
449
491
450
492
<br />
451
493
452
-
## Custom initialization / re-initialization
453
-
454
-
If the targets array is empty, _useActive_ won't initialize the scroll observer.
455
-
456
-
Whenever `root` or `targets` are updated (and not empty), _useActive_ will re-initialize the observer.
0 commit comments