Skip to content

Commit 9ce5c1f

Browse files
committed
docs: add scroll-cache md
1 parent 92ba62f commit 9ce5c1f

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

src/pages/scroll-cache/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Page Scroll Position Save and Restore Guide
2+
3+
If you want to save the current scroll position when leaving a page and restore it upon return, you can follow the approach outlined below.
4+
5+
## Basic Approach
6+
7+
1. **Cache the Component**:
8+
Set `keepAlive` to `true` to cache the component.
9+
10+
2. **Save Scroll Position**:
11+
Use the `onBeforeRouteLeave` hook to save the current scroll position when leaving the page.
12+
13+
3. **Restore Scroll Position**:
14+
Use the `onActivated` hook to restore the last saved scroll position when the page is activated.
15+
16+
## Example Code
17+
18+
```js
19+
// Define a ref to store the scroll position
20+
const scrollTop = ref(0)
21+
22+
// When a component with keepAlive set to true is activated, scroll to the saved position
23+
onActivated(() => {
24+
window.scrollTo(0, scrollTop.value)
25+
})
26+
27+
// Before leaving the route, save the current scroll position
28+
onBeforeRouteLeave(() => {
29+
scrollTop.value
30+
= window.scrollY
31+
|| document.documentElement.scrollTop
32+
|| document.body.scrollTop
33+
})
34+
```
35+
36+
# Handling a Specific Scroll Container
37+
38+
If you need to save and restore the scroll position for a specific element (instead of the entire window), follow these steps:
39+
40+
## 1. Add a ref in the Template
41+
42+
In your template, add a `ref` attribute to the scroll container element. For example:
43+
44+
```html
45+
<div ref="scrollContainer" class="...">...</div>
46+
47+
```
48+
49+
## 2. In the setup Function
50+
51+
Use a ref to obtain the element's reference:
52+
53+
```js
54+
const scrollContainer = ref(null)
55+
```
56+
57+
## 3. In the onBeforeRouteLeave Hook
58+
59+
Save the scroll container's scroll position:
60+
61+
```js
62+
onBeforeRouteLeave(() => {
63+
if (scrollContainer.value) {
64+
scrollTop.value = scrollContainer.value.scrollTop
65+
}
66+
})
67+
```
68+
69+
## 3. In the onActivated Hook
70+
71+
Restore the scroll container's scroll position:
72+
73+
```js
74+
onActivated(() => {
75+
if (scrollContainer.value) {
76+
scrollContainer.value.scrollTop = scrollTop.value
77+
}
78+
})
79+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 页面滚动位置保存与恢复指南
2+
3+
如果你希望在离开页面时保存当前的滚动位置,并在返回时恢复它,可以参考下面的实现思路:
4+
5+
## 基本思路
6+
7+
1. **缓存组件**
8+
设置 `keepAlive``true` 以缓存组件。
9+
10+
2. **保存滚动位置**
11+
在页面离开时,使用 `onBeforeRouteLeave` 钩子保存当前滚动位置。
12+
13+
3. **恢复滚动位置**
14+
在页面激活时,使用 `onActivated` 钩子恢复上次的滚动位置。
15+
16+
## 示例代码
17+
18+
```js
19+
// 定义一个 ref 用于存储滚动位置
20+
const scrollTop = ref(0)
21+
22+
// 当 keepAlive 为 true 的组件被激活时,滚动到保存的位置
23+
onActivated(() => {
24+
window.scrollTo(0, scrollTop.value)
25+
})
26+
27+
// 在路由离开前,保存当前的滚动位置
28+
onBeforeRouteLeave(() => {
29+
scrollTop.value
30+
= window.scrollY
31+
|| document.documentElement.scrollTop
32+
|| document.body.scrollTop
33+
})
34+
```
35+
36+
# 针对指定滚动容器的处理
37+
38+
如果你需要对特定元素(而非整个窗口)进行滚动位置的保存和恢复,可按以下步骤操作:
39+
40+
## 1. 在 Template 中添加 ref
41+
42+
在模板中,给滚动容器元素添加 `ref` 属性。例如:
43+
44+
```html
45+
<div ref="scrollContainer" class="...">...</div>
46+
```
47+
48+
## 2. 在 setup 中:
49+
50+
使用 ref 获取该元素的引用:
51+
52+
```js
53+
const scrollContainer = ref(null)
54+
```
55+
56+
## 3. 在 onBeforeRouteLeave 钩子中:
57+
58+
保存滚动容器的滚动位置:
59+
60+
```js
61+
onBeforeRouteLeave(() => {
62+
if (scrollContainer.value) {
63+
scrollTop.value = scrollContainer.value.scrollTop
64+
}
65+
})
66+
```
67+
68+
## 3. 在 onActivated 钩子中:
69+
70+
恢复滚动容器的滚动位置:
71+
72+
```js
73+
onActivated(() => {
74+
if (scrollContainer.value) {
75+
scrollContainer.value.scrollTop = scrollTop.value
76+
}
77+
})
78+
```
79+
80+
万事 OK 👌🏻

0 commit comments

Comments
 (0)