当前位置:首页 > 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 节点数量。

安装依赖:

npm install vue-virtual-scroller

示例代码:

<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 或媒体查询实现不同屏幕尺寸下的列表布局调整。

vue实现纵向列表

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

jquery列表

jquery列表

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

vue实现列表分类

vue实现列表分类

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

vue实现列表点击

vue实现列表点击

Vue 实现列表点击的方法 在 Vue 中实现列表点击功能通常涉及 v-for 渲染列表和 v-on(或 @)绑定点击事件。以下是几种常见实现方式: 基础列表点击 使用 v-for 渲染列表,并通过…

vue实现树形列表

vue实现树形列表

Vue 实现树形列表的方法 递归组件实现 使用递归组件是最常见的方式,适合处理嵌套层级不确定的数据结构。 数据结构示例 data() { return { treeData: […

vue 实现列表更换

vue 实现列表更换

实现列表更换的方法 在Vue中实现列表更换通常涉及数据绑定和响应式更新。以下是几种常见的方法: 使用v-for指令绑定数组 通过修改数据源数组实现列表内容的动态更换。Vue的响应式系统会自动更新DO…