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
Copy file name to clipboardExpand all lines: README.md
+75-24Lines changed: 75 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,7 @@
14
14
The [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) is a great API.
15
15
But it may not be the one-size-fits-all solution to highlight menu/sidebar links.
16
16
17
-
You may noticed that's tricky to customize behavior according to different interactions.
18
-
19
-
For example, you want to immediately highlight targets when scroll is originated from click/navigation but not when it is originated from wheel/touch. You also want to highlight any clicked link even if it will never intersect.
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.
20
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
20
@@ -26,12 +24,14 @@ For example, you want to immediately highlight targets when scroll is originated
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
+
95
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.
| jumpToFirst |`boolean`| true | Whether to set the first target on mount as active even if not (yet) intersecting. |
160
-
| jumpToLast |`boolean`| true | Whether to set the last target as active once reached the bottom even if previous targets are entirely visible. |
161
-
| boundaryOffset |`BoundaryOffset`| { toTop: 0, toBottom: 0 } | Boundary offset in px for each scroll direction. Tweak them to "anticipate" or "delay" target detection. |
162
-
| rootId |`string`\|`null`| null | Id of the scrolling element. Set it only if your content **is not scrolled** by the window. |
163
-
| replaceHash |`boolean`| false | Whether to replace URL hash on scroll. First target is ignored if `jumpToFirst` is true. |
164
-
| overlayHeight |`number`| 0 | Height in pixels of any **CSS fixed** content that overlaps the top of your scrolling area (e.g. fixed header). Must be paired with a CSS [scroll-margin-top](#setting-scroll-margin-top-for-fixed-headers) rule. |
165
-
| minWidth |`number`| 0 | Whether to toggle listeners and functionalities within a specific width. Useful if hiding the sidebar using `display: none`. |
| jumpToFirst |`boolean`| true | Whether to set the first target on mount as active even if not (yet) intersecting. |
160
+
| jumpToLast |`boolean`| true | Whether to set the last target as active once reached the bottom even if previous targets are entirely visible. |
161
+
| boundaryOffset |`BoundaryOffset`| { toTop: 0, toBottom: 0 } | Boundary offset in px for each scroll direction. Tweak them to "anticipate" or "delay" target detection. |
162
+
| edgeOffset |`EdgeOffset`| { first: 100, last: -100 } | Offset in px for fist and last target. `first` has no effect if `jumpToFirst` is true. Same for `last` if `jumpToLast` is true. |
163
+
| root |`HTMLElement \| null`\|`Ref<HTMLElement \| null>`| null | Scrolling element. Set it only if your content **is not scrolled** by the window. If _null_, defaults to documentElement. |
164
+
| replaceHash |`boolean`| false | Whether to replace URL hash on scroll. First target is ignored if `jumpToFirst` is true. |
165
+
| overlayHeight |`number`| 0 | Height in pixels of any **CSS fixed** content that overlaps the top of your scrolling area (e.g. fixed header). Must be paired with a CSS [scroll-margin-top](#setting-scroll-margin-top-for-fixed-headers) rule. |
166
+
| minWidth |`number`| 0 | Whether to toggle listeners and functionalities within a specific width. Useful if hiding the sidebar using `display: none`. |
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).
210
211
211
-
#### A. Using CSS scroll-behavior
212
+
#### A. Using native CSS scroll-behavior
212
213
213
214
- If content is scrolled by the window, add the following CSS rule to your `html` element:
214
215
@@ -279,6 +280,7 @@ function scrollTo(event, id) {
## Vue Router - Scroll to hash onMount / navigation
371
373
372
374
> :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.
373
375
@@ -399,7 +401,7 @@ const router = createRouter({
399
401
400
402
> :bulb: There's no need need to set overlayHeight if using `scrollIntoView` as the method is aware of target's `scroll-margin-top` property.
401
403
402
-
### Scrolling from hash to the top of the page
404
+
### Scrolling from hash back to the top of the page
403
405
404
406
To navigate back to the top of the same page (e.g. clicking on browser back button from a hash to the page root), use the _scroll_ method for containers and return _top_ for content scrolled by the window.
You may noticed that when clicking on a link, a new entry is added to the history. When navigating back, the page will scroll to the previous target and so on.
433
+
434
+
If you don't like that, choose to replace instead of pushing the hash:
0 commit comments