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

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象进行渲染。以下是几种常见的实现方式: 基础列表渲染 通过 v-for 指令遍历数组,动态生成列表项。 &…

h5 实现列表

h5 实现列表

列表实现方法 在HTML5中,可以通过多种方式实现列表,包括无序列表、有序列表和自定义列表。以下是具体的实现方法。 无序列表 无序列表使用<ul>标签,列表项使用<li>标…

vue实现列表添加

vue实现列表添加

使用 Vue 实现列表添加功能 数据绑定与列表渲染 在 Vue 中,通过 v-for 指令可以轻松实现列表渲染。首先需要定义一个数组来存储列表数据,并在模板中使用 v-for 循环渲染。 <t…

jquery列表

jquery列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>),包括动态添加、删除、修改列表项,以及事件绑定等。 创建列表 使用…