面试官:如何实现在浏览商品时,进入详情,再返回,列表刷新,滚动条还在之前的位置?

本文介绍了如何在Vue应用中,通过记录和恢复滚动条位置,实现在商品详情页点击后返回列表页时列表自动刷新且保持之前滚动位置的技术,包括基本方法、Vue框架实现以及使用keep-alive组件优化。
摘要由CSDN通过智能技术生成

哈喽,大家好,好久没发过文章了,最近工作上有点忙。 现在有这样一个场景题:
在浏览商品时,点击商品详情,然后返回,要求列表刷新,同时滚动条还在之前的位置,怎么实现?

一、基本方法

思路

  1. 在点击商品详情进入商品详情页之前,记录当前滚动条的位置。
  2. 当从商品详情页返回列表页时,触发列表的刷新操作。
  3. 在列表刷新完成后,将之前记录的滚动条位置重新设置回去。
// 记录当前滚动条位置
var scrollPosition = window.scrollY;
// 点击商品详情进入详情页的操作
function gotoProductDetails(productId) {
  // 保存当前滚动条位置
  sessionStorage.setItem('scrollPosition', scrollPosition);
  // 其他跳转逻辑...
}

// 从商品详情页返回列表页的操作
function goBackToList() {
  // 触发列表刷新操作
  refreshList();
  // 恢复之前的滚动条位置
  var savedScrollPosition = sessionStorage.getItem('scrollPosition');
  window.scrollTo(0, savedScrollPosition);
}

// 列表刷新操作
function refreshList() {
  // 刷新列表的逻辑...
}

使用sessionStorage来存储和恢复滚动条位置。当用户点击商品详情时,我们记录当前的滚动条位置,然后在返回列表页时,首先触发列表的刷新操作,然后再将之前记录的滚动条位置重新设置回去。

二、Vue框架实现

在Vue框架中实现这个功能可以使用Vue Router和VueX技术。

  1. 首先,你需要在Vue项目中使用Vue Router进行路由管理。确保你有正确设置了商品详情页和列表页的路由。
  2. 在进入商品详情页之前,你可以使用VueX来存储当前滚动条位置。
// main.js

import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app');
  1. 当从商品详情页返回列表页时,你可以触发列表的刷新操作,并使用VueX来获取之前存储的滚动条位置并将其重新设置回去。
<!-- ProductDetail.vue -->

<template>
  <div>
    <!-- 商品详情页内容 -->
    <button @click="goBack">返回</button>
  </div>
</template>

<script>
export default {
  methods: {
    goBack() {
      this.$store.dispatch('saveScrollPosition', window.scrollY);
      this.$router.push('/list'); // 返回列表页
    }
  }
}
</script>
<!-- List.vue -->

<template>
  <div>
    <!-- 列表页内容 -->
  </div>
</template>

<script>
export default {
  mounted() {
    this.$store.dispatch('restoreScrollPosition');
    // 触发列表的刷新操作
    // ...
  }
}
</script>
  1. 在VueX store 中保存和恢复滚动条位置。
// store/index.js

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    scrollPosition: 0
  },
  mutations: {
    SET_SCROLL_POSITION(state, position) {
      state.scrollPosition = position;
    }
  },
  actions: {
    saveScrollPosition({ commit }, position) {
      commit('SET_SCROLL_POSITION', position);
    },
    restoreScrollPosition({ state }) {
      window.scrollTo(0, state.scrollPosition);
    }
  }
});

利用Vue Router来管理页面跳转,使用了VueX来存储和恢复滚动条位置。当用户点击商品详情页面时,使用saveScrollPosition action来保存当前滚动条位置,在返回列表页时,再使用restoreScrollPosition action来恢复之前的滚动条位置

三、keep-alive实现

使用Vue框架中的keep-alive件可以在列表刷新时保持滚动条的位置。

<!-- App.vue -->

<template>
  <div id="app">
    <router-view v-slot="{ Component }">
      <keep-alive :include="['List']">
        <component :is="Component" />
      </keep-alive>
    </router-view>
  </div>
</template>

<script>
export default {
  name: 'App'
};
</script>
<!-- List.vue -->

<template>
  <div>
    <!-- 列表页内容 -->
  </div>
</template>

<script>
export default {
  name: 'List',
  beforeRouteLeave (to, from, next) {
    const scrollPosition = window.scrollY;
    this.$store.commit('SET_SCROLL_POSITION', { component: 'List', position: scrollPosition });
    next();
  },
  updated() {
    const scrollPosition = this.$store.state.scrollPositions['List'];
    if (scrollPosition !== undefined) {
      window.scrollTo(0, scrollPosition);
    }
  }
};
</script>

// store/index.js

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    scrollPositions: {}
  },
  mutations: {
    SET_SCROLL_POSITION(state, payload) {
      state.scrollPositions[payload.component] = payload.position;
    }
  }
});

使用keep-alive组件包裹了<router-view>,并设置了其中的List组件为缓存组件。当用户离开List页面时,记录当前滚动条位置并将其保存在Vuex store 中。然后,在List组件更新时,检查是否有之前保存的滚动条位置,并将滚动条位置恢复到之前的状态。

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 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 移动端的列表进入详情页后,保持原来滚动位置。需要注意的是,如果列表页中有异步加载的数据或者图片,可能会影响滚动位置的计算,需要特别注意。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值