当前位置:首页 > 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

示例代码:

<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实现索引列表

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

相关文章

h5实现展开列表

h5实现展开列表

使用HTML5和CSS实现展开列表 在HTML5中,可以通过结合CSS和JavaScript实现展开列表效果。以下是几种常见的方法: 方法1:使用details和summary标签 HTML5提供…

vue实现奇偶列表

vue实现奇偶列表

Vue实现奇偶列表样式 在Vue中实现列表项的奇偶不同样式,可以通过多种方式实现。以下是几种常见方法: 使用v-for和动态class绑定 通过v-for循环生成列表时,利用索引值判断奇偶性并…

vue实现列表分类

vue实现列表分类

Vue 实现列表分类的方法 使用计算属性分类 通过计算属性对原始数组进行分类处理,返回分组后的对象或数组。适用于静态数据或需要响应式更新的场景。 computed: { categorized…

vue实现索引栏

vue实现索引栏

Vue 实现索引栏的方法 索引栏通常用于快速导航或分类,常见于通讯录、城市列表等场景。以下是几种实现方式: 基于滚动事件的索引栏 数据结构准备 将数据按字母分组,例如: const cont…

vue列表展开实现

vue列表展开实现

Vue 列表展开实现方法 使用 v-for 和 v-show 控制展开状态 通过 v-for 渲染列表项,配合 v-show 控制子内容的显示隐藏。需要为每个列表项维护一个展开状态。 <te…

vue 实现商品列表

vue 实现商品列表

实现商品列表的基本结构 在Vue中实现商品列表,通常需要创建一个组件来展示商品数据。可以使用v-for指令循环渲染商品列表,并通过数据绑定动态显示商品信息。 <template> &…