当前位置:首页 > 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实现索引列表

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

相关文章

vue实现商品列表

vue实现商品列表

Vue实现商品列表的方法 使用Vue实现商品列表需要结合数据绑定、组件化和状态管理。以下是实现商品列表的几种常见方法: 基础数据绑定实现 在Vue组件中定义商品数据数组,使用v-for指令循环渲染…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 在Vue中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或图…

vue 实现长列表

vue 实现长列表

vue 实现长列表的优化方法 使用虚拟滚动技术,只渲染可视区域内的元素,大幅减少DOM节点数量。通过计算滚动位置动态更新显示内容,降低内存占用和渲染压力。 <template> &l…

vue实现列表显示

vue实现列表显示

使用 Vue 实现列表显示 基础列表渲染 在 Vue 中,可以通过 v-for 指令实现列表渲染。以下是一个简单的示例: <template> <ul> <…

h5实现展开列表

h5实现展开列表

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

vue实现列表

vue实现列表

实现列表的基本方法 在Vue中实现列表通常使用v-for指令,这是Vue的核心功能之一。v-for可以遍历数组或对象,为每个元素生成对应的DOM节点。 <template> <…