Skip to content
/ router Public

docs: add advanced offsets content for scrollBehavoir #2448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/docs/guide/advanced/scroll-behavior.md
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ const router = createRouter({
behavior: 'smooth',
}
}
}
},
})
```

@@ -113,3 +113,29 @@ const router = createRouter({
```

It's possible to hook this up with events from a page-level transition component to make the scroll behavior play nicely with your page transitions, but due to the possible variance and complexity in use cases, we simply provide this primitive to enable specific userland implementations.

## Advanced offsets

If your page has a fixed navbar or similar elements, you might need an offset to ensure the target element isn't hidden behind other content.
Using a static offset value may not always work. You might try CSS-based solutions, like adding offsets with `scroll-margin` or `scroll-padding`, or using `::before` and `::after` pseudo-elements. However, these approaches can lead to unexpected behavior.

In such cases, it's better to calculate the offset manually. A simple way to do this is by combining CSS with JavaScript's `getComputedStyle()`. This lets each element define its own offset dynamically. Here's an example:

```js
const router = createRouter({
scrollBehavior(to, from, savedPosition) {
const mainElement = document.querySelector('#main')
if (mainElement) {
const marginTop = parseFloat(
getComputedStyle(mainElement).scrollMarginTop
)
return {
el: mainElement,
top: marginTop,
}
} else {
return { top: 0 }
}
},
})
```
31 changes: 29 additions & 2 deletions packages/docs/zh/guide/advanced/scroll-behavior.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 滚动行为

<VueSchoolLink
href="https://vueschool.io/lessons/scroll-behavior"
title="Learn how to customize scroll behavior"
@@ -20,7 +21,7 @@ const router = createRouter({
})
```

`scrollBehavior` 函数接收 `to`` from` 路由对象,如 [Navigation Guards](./navigation-guards.md)。第三个参数 `savedPosition`,只有当这是一个 `popstate` 导航时才可用(由浏览器的后退/前进按钮触发)。
`scrollBehavior` 函数接收 `to``from` 路由对象,如 [Navigation Guards](./navigation-guards.md)。第三个参数 `savedPosition`,只有当这是一个 `popstate` 导航时才可用(由浏览器的后退/前进按钮触发)。

该函数可以返回一个 [`ScrollToOptions`](https://developer.mozilla.org/en-US/docs/Web/API/ScrollToOptions) 位置对象:

@@ -91,7 +92,7 @@ const router = createRouter({
behavior: 'smooth',
}
}
}
},
})
```

@@ -112,3 +113,29 @@ const router = createRouter({
```

我们可以将其与页面级过渡组件的事件挂钩,以使滚动行为与你的页面过渡很好地结合起来,但由于使用场景可能存在的差异和复杂性,我们只是提供了这个基础来实现特定的用户场景。

## 高级偏移量

如果你的页面中有固定的导航栏或类似的元素,你可能需要设置偏移量,以确保目标元素不会被其他内容遮挡。
使用静态偏移值并不总是有效。你可以尝试一些基于 CSS 的解决方案,比如使用 `scroll-margin``scroll-padding` 添加偏移,或者使用 `::before``::after` 伪元素。然而,这些方法有时会导致意想不到的行为。

在这种情况下,更好的做法是手动计算偏移量。一种简单的方法是结合 CSS 和 JavaScript 的 `getComputedStyle()`。这样每个元素都可以动态定义自己的偏移量。以下是一个示例:

```js
const router = createRouter({
scrollBehavior(to, from, savedPosition) {
const mainElement = document.querySelector('#main')
if (mainElement) {
const marginTop = parseFloat(
getComputedStyle(mainElement).scrollMarginTop
)
return {
el: mainElement,
top: marginTop,
}
} else {
return { top: 0 }
}
},
})
```