vue移动端从列表详情返回列表页之前的位置(包括分页情况)

前提:vue的移动端分页,从列表进入详情后返回后直接回到了当前列表页顶部,而不是回到之前的位置,影响用户体验

解决方案:使用keepalive加上router

具体步骤:router中用meta设置变量scrollTop存储当前的滑动位置,默认为0,导航守卫中给其赋值,然后在需要的页面中用actived钩子重新给当前页面的scrollTop赋值为meta中记录的scrollTop

代码

router.js

{
        path: 'yourPage',
        name: 'yourPage',
        component: () => import('@views/yourPage.vue'),
        meta: {
          title: '我的页面',
          keepAlive: true,
          scrollTop: 0
        }
},
router.beforeEach((to,from,next) => {
  // 记录缓存页之前的位置,从详情页返回后可以直接跳转回当前位置
  if(from.meta.keepAlive) {
    const $content = document.getElementById('app')
    const scrollTop = $content?.scrollTop
    from.meta.scrollTop = scrollTop || 0
  }
  next()
})

yourPage.vue

// 跳转到进入详情页之前的位置
  activated() {
    const $content = document.querySelector('#app')
    $content.scrollTop = this.$route.meta.scrollTop
  },

记得设置keepAlive,否则不起作用

keepAlive相关配置

APP.vue

     <keep-alive>
        <router-view v-if="$route.meta.keepAlive" />
      </keep-alive>
      <router-view v-if="!$route.meta.keepAlive" />

方法二:

yourPage.vue

beforeRouteLeave(to, from, next) {
    //保存滚动条元素div的scrollTop值
    this.scrollY = document.getElementById("app").scrollTop;
    next();
  },
  // 为div元素重新设置保存的scrollTop值
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.$nextTick(() => {
        document.getElementById("app").scrollTop = vm.scrollY || 0;
      });
    });
  },

方法三:scrollBehavior

router.js中

const scrollBehavior = function scrollBehavior (to, from, savedPosition) {
  if (savedPosition) {
    return savedPosition;
  }else {
      return { x: 0, y: 0 }
   }
};
const router = new Router({
  routes,
  scrollBehavior,
});


 

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue3 移动端列表进入详情页后,为了让用户体验更加流畅,通常需要在用户返回列表时保持原来的滚动位置。下面是一种实现方式: 1. 在列表中记录当前滚动位置 我们可以在列表的 mounted 钩子函数中记录当前滚动位置,然后在用户返回列表时,将面滚动到该位置。 ```js <template> <div ref="listContainer"> <!-- 列表内容 --> </div> </template> <script> export default { mounted() { // 记录当前滚动位置 this.scrollPosition = 0; this.$refs.listContainer.addEventListener('scroll', this.onScroll); }, beforeUnmount() { // 移除滚动事件监听器 this.$refs.listContainer.removeEventListener('scroll', this.onScroll); }, methods: { onScroll() { // 更新滚动位置 this.scrollPosition = this.$refs.listContainer.scrollTop; }, }, }; </script> ``` 2. 在详情页中记录返回时的滚动位置 当用户进入详情页时,我们需要记录用户返回列表时的滚动位置。我们可以使用路由的 meta 属性来记录该位置,然后在用户返回列表时,从 meta 中获取该位置。 ```js const router = createRouter({ routes: [ { path: '/list', component: List, }, { path: '/detail/:id', component: Detail, meta: { scrollPosition: 0, }, }, ], }); <script> export default { mounted() { // 将返回时的滚动位置保存到路由的 meta 中 this.$route.meta.scrollPosition = this.$refs.detailContainer.scrollTop; }, }; </script> ``` 3. 在列表中恢复滚动位置 当用户从详情页返回列表时,我们需要从路由的 meta 中获取返回时的滚动位置,然后将面滚动到该位置。 ```js <template> <div ref="listContainer"> <!-- 列表内容 --> </div> </template> <script> export default { mounted() { // 从路由的 meta 中获取返回时的滚动位置 this.scrollPosition = this.$route.meta.scrollPosition || 0; this.$refs.listContainer.addEventListener('scroll', this.onScroll); }, beforeUnmount() { // 保存滚动位置到路由的 meta 中 this.$route.meta.scrollPosition = this.$refs.listContainer.scrollTop; // 移除滚动事件监听器 this.$refs.listContainer.removeEventListener('scroll', this.onScroll); }, methods: { onScroll() { // 更新滚动位置 this.scrollPosition = this.$refs.listContainer.scrollTop; }, }, activated() { // 面重新激活时,将面滚动到之前位置 this.$refs.listContainer.scrollTop = this.scrollPosition; }, }; </script> ``` 通过上述步骤,我们就可以在 Vue3 移动端列表进入详情页后,保持原来的滚动位置。需要注意的是,如果列表中有异步加载的数据或者图片,可能会影响滚动位置的计算,需要特别注意。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值