因此,您希望以某种方式vuejs记住滚动位置。与往常一样,vue非常简单。
滚动行为可由Vue路由器管理。我给你举个例子。
具有2个滚动位置的页面组件
<template>
<div>
<div id="data1" style="height:700px;background:blue">
First scroll position
</div>
<div id="data2" style="height:700px;background:yellow">
Second scroll position
</div>
</div>
</template>
导航到页面组件的组件
<template>
<div>
<router-link
tag="button"
:to="link"
>
Navigate to Page in scroll Position 2
</router-link>
</div>
</template>
<script>
export default {
data: () => ({
link: {
name: 'Page',
hash: '#data2'
}
})
}
</script>
现在,您的路由文件必须如下所示:
import Vue from 'vue'
import Router from 'vue-router'
import Page from '@/components/Page.vue'
import NavigationButton from '@/components/NavigationButton.vue'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/Page',
name: 'Page',
component: Page,
},
{
path: '/',
name: 'NavigationButton',
component: NavigationButton
},
],
mode: 'history',
scrollBehavior(to, from, savedPosition) {
if(savedPosition){
return savedPosition
}
if(to.hash){
return { selector: to.hash }
}
return { x:0, y: 0 }
}
})
我理解你的问题,但我不确定你问的是什么。无论如何,如果我是对的,别忘了看看关于滚动行为的vue路由器官方文档。
See here