当前位置:首页 > VUE

vue实现索引列表

2026-01-18 07:01:03VUE

Vue 实现索引列表

使用第三方库(如 better-scrollvue-index-list

安装 better-scrollvue-index-list 库,可以快速实现带索引的列表功能。以 better-scroll 为例:

npm install better-scroll --save

在 Vue 组件中引入并使用:

<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <div v-for="(group, index) in data" :key="index" class="group">
        <div class="title">{{ group.title }}</div>
        <div v-for="(item, idx) in group.items" :key="idx" class="item">{{ item }}</div>
      </div>
    </div>
    <div class="index-list">
      <div v-for="(group, index) in data" :key="index" @click="scrollTo(index)">
        {{ group.title }}
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll';

export default {
  data() {
    return {
      data: [
        { title: 'A', items: ['Apple', 'Ant'] },
        { title: 'B', items: ['Banana', 'Bear'] },
        // 更多数据...
      ],
      scroll: null
    };
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper, {
      probeType: 3
    });
  },
  methods: {
    scrollTo(index) {
      const group = this.$refs.wrapper.querySelectorAll('.group')[index];
      this.scroll.scrollToElement(group, 300);
    }
  }
};
</script>

<style>
.wrapper {
  height: 100vh;
  overflow: hidden;
  position: relative;
}
.index-list {
  position: fixed;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.1);
  padding: 10px 5px;
  border-radius: 10px;
}
</style>

自定义实现索引列表

如果需要手动实现索引列表,可以通过监听滚动事件和计算当前滚动位置来实现:

<template>
  <div class="list-container" @scroll="handleScroll">
    <div v-for="(group, index) in data" :key="index" class="group" :ref="`group-${index}`">
      <div class="title">{{ group.title }}</div>
      <div v-for="(item, idx) in group.items" :key="idx" class="item">{{ item }}</div>
    </div>
    <div class="index-indicator" v-if="currentIndex !== null">
      {{ data[currentIndex].title }}
    </div>
    <div class="index-list">
      <div v-for="(group, index) in data" :key="index" @click="scrollTo(index)">
        {{ group.title }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [
        { title: 'A', items: ['Apple', 'Ant'] },
        { title: 'B', items: ['Banana', 'Bear'] },
        // 更多数据...
      ],
      currentIndex: null
    };
  },
  methods: {
    handleScroll(e) {
      const scrollTop = e.target.scrollTop;
      this.data.forEach((group, index) => {
        const groupEl = this.$refs[`group-${index}`][0];
        const groupTop = groupEl.offsetTop;
        const groupHeight = groupEl.offsetHeight;
        if (scrollTop >= groupTop && scrollTop < groupTop + groupHeight) {
          this.currentIndex = index;
        }
      });
    },
    scrollTo(index) {
      const groupEl = this.$refs[`group-${index}`][0];
      this.$el.scrollTo({
        top: groupEl.offsetTop,
        behavior: 'smooth'
      });
    }
  }
};
</script>

<style>
.list-container {
  height: 100vh;
  overflow-y: auto;
  position: relative;
}
.index-indicator {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
  z-index: 100;
}
.index-list {
  position: fixed;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.1);
  padding: 10px 5px;
  border-radius: 10px;
}
</style>

使用 Vue 插件(如 vue-virtual-scroll-list

对于大数据量的索引列表,可以使用虚拟滚动优化性能:

npm install vue-virtual-scroll-list --save

示例代码:

vue实现索引列表

<template>
  <virtual-list
    :size="50"
    :remain="10"
    :data-key="'title'"
    :data-sources="data"
    @scroll="handleScroll"
  >
    <template v-slot="{ source }">
      <div class="group">
        <div class="title">{{ source.title }}</div>
        <div v-for="(item, idx) in source.items" :key="idx" class="item">{{ item }}</div>
      </div>
    </template>
  </virtual-list>
  <div class="index-list">
    <div v-for="(group, index) in data" :key="index" @click="scrollTo(index)">
      {{ group.title }}
    </div>
  </div>
</template>

<script>
import VirtualList from 'vue-virtual-scroll-list';

export default {
  components: { VirtualList },
  data() {
    return {
      data: [
        { title: 'A', items: ['Apple', 'Ant'] },
        { title: 'B', items: ['Banana', 'Bear'] },
        // 更多数据...
      ]
    };
  },
  methods: {
    scrollTo(index) {
      // 需要根据插件 API 实现滚动到指定位置
    }
  }
};
</script>

以上方法可以根据需求选择适合的方式实现索引列表。

标签: 索引列表
分享给朋友:

相关文章

vue实现列表分页

vue实现列表分页

Vue 列表分页实现方法 基础分页实现 安装依赖(如使用第三方库) npm install vue-paginate 模板部分示例 <template> <div>…

vue实现新建列表

vue实现新建列表

Vue 实现新建列表的方法 在 Vue 中实现新建列表功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态渲染列表 通过数据驱动视图的方式,利用 v-for 指令动态渲染列表项…

vue实现题目列表

vue实现题目列表

Vue实现题目列表的方法 数据准备 在Vue中实现题目列表需要先准备题目数据。通常将题目数据存储在组件的data或通过API从后端获取。示例数据结构如下: data() { return {…

vue实现分类列表

vue实现分类列表

Vue 实现分类列表的方法 数据结构设计 使用数组存储分类数据,每个分类对象包含 id、name 等属性,子分类通过 children 字段嵌套。例如: categories: [ {…

vue实现信息列表

vue实现信息列表

Vue 实现信息列表的方法 使用 v-for 渲染列表 通过 Vue 的 v-for 指令可以轻松渲染动态列表数据。在模板中使用 v-for 遍历数组,并为每个项生成对应的 DOM 元素。 <…

轮播列表vue怎么实现

轮播列表vue怎么实现

使用 Vue 实现轮播列表 基于 Swiper 插件实现 安装 Swiper 依赖: npm install swiper 引入 Swiper 和样式: import { Swiper, Swip…