当前位置:首页 > VUE

vue实现纵向列表

2026-02-18 05:00:55VUE

实现纵向列表的基本方法

使用 Vue 实现纵向列表可以通过 v-for 指令动态渲染数组数据。基本结构包括一个外层容器和循环生成的列表项。

<template>
  <div class="list-container">
    <div v-for="(item, index) in items" :key="index" class="list-item">
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { text: 'Item 1' },
        { text: 'Item 2' },
        { text: 'Item 3' }
      ]
    };
  }
};
</script>

<style>
.list-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.list-item {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
</style>

动态加载更多数据

对于长列表,可以通过监听滚动事件实现无限滚动加载。使用 @scroll 事件和计算逻辑判断是否触底。

<template>
  <div class="scroll-container" @scroll="handleScroll">
    <div v-for="(item, index) in items" :key="index" class="list-item">
      {{ item.text }}
    </div>
    <div v-if="loading" class="loading">Loading...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      loading: false,
      page: 1
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.loading = true;
      const newItems = await api.fetchItems(this.page);
      this.items = [...this.items, ...newItems];
      this.page++;
      this.loading = false;
    },
    handleScroll(e) {
      const { scrollTop, clientHeight, scrollHeight } = e.target;
      if (scrollHeight - (scrollTop + clientHeight) < 50 && !this.loading) {
        this.fetchData();
      }
    }
  }
};
</script>

使用第三方库优化性能

对于超长列表(如上千项),建议使用虚拟滚动库(如 vue-virtual-scroller)减少 DOM 节点数量。

vue实现纵向列表

安装依赖:

npm install vue-virtual-scroller

示例代码:

vue实现纵向列表

<template>
  <RecycleScroller
    class="scroller"
    :items="items"
    :item-size="50"
    key-field="id"
    v-slot="{ item }"
  >
    <div class="item">
      {{ item.text }}
    </div>
  </RecycleScroller>
</template>

<script>
import { RecycleScroller } from 'vue-virtual-scroller';
export default {
  components: { RecycleScroller },
  data() {
    return {
      items: [] // 需包含唯一id字段
    };
  }
};
</script>

<style>
.scroller {
  height: 400px;
}
.item {
  height: 50px;
  padding: 10px;
}
</style>

添加动画效果

通过 Vue 的过渡组件实现列表项入场动画。

<template>
  <div class="list">
    <transition-group name="fade">
      <div v-for="item in items" :key="item.id" class="item">
        {{ item.text }}
      </div>
    </transition-group>
  </div>
</template>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
.item {
  margin: 5px 0;
}
</style>

响应式布局适配

结合 CSS Grid 或媒体查询实现不同屏幕尺寸下的列表布局调整。

.list-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 15px;
}

@media (min-width: 768px) {
  .list-container {
    grid-template-columns: repeat(2, 1fr);
  }
}

标签: 纵向列表
分享给朋友:

相关文章

vue列表查询实现

vue列表查询实现

实现Vue列表查询功能 基本数据绑定与渲染 在Vue中实现列表查询,首先需要定义数据源和查询条件。通过v-model绑定搜索输入框,使用计算属性过滤列表。 <template> &…

jquery 列表

jquery 列表

jQuery 列表操作 jQuery 提供了多种方法来操作 HTML 列表(如 <ul> 或 <ol>)。以下是一些常见的操作方式: 动态添加列表项 使用 append()…

vue实现列表分页

vue实现列表分页

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

vue实现列表组件

vue实现列表组件

Vue 列表组件实现方法 基础列表渲染 使用 v-for 指令渲染数组数据,需配合 :key 提升性能: <template> <ul> <li v-for…

vue实现列表多选

vue实现列表多选

Vue 实现列表多选的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以轻松实现多选功能。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现列表过渡

vue实现列表过渡

vue实现列表过渡的方法 在Vue中,可以使用内置的<transition-group>组件来实现列表项的过渡效果。该组件专门为动态列表设计,能够处理元素的添加、删除和顺序变化。 基本…