当前位置:首页 > VUE

vue实现滚动显示列表

2026-02-25 07:42:09VUE

使用 v-for 和 CSS 实现滚动列表

在 Vue 中,可以通过 v-for 渲染列表数据,结合 CSS 的 overflow 和滚动样式实现滚动效果。

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

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

<style>
.scroll-container {
  height: 200px;
  overflow-y: auto;
  border: 1px solid #ddd;
}
.list-item {
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>

使用 setInterval 实现自动滚动

通过 JavaScript 定时器控制滚动容器的 scrollTop 属性,实现自动滚动效果。

vue实现滚动显示列表

<template>
  <div ref="scrollContainer" class="scroll-container">
    <div v-for="(item, index) in list" :key="index" class="list-item">
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'],
      scrollInterval: null
    };
  },
  mounted() {
    this.startAutoScroll();
  },
  beforeDestroy() {
    this.stopAutoScroll();
  },
  methods: {
    startAutoScroll() {
      this.scrollInterval = setInterval(() => {
        const container = this.$refs.scrollContainer;
        if (container.scrollTop >= container.scrollHeight - container.clientHeight) {
          container.scrollTop = 0;
        } else {
          container.scrollTop += 1;
        }
      }, 50);
    },
    stopAutoScroll() {
      if (this.scrollInterval) {
        clearInterval(this.scrollInterval);
      }
    }
  }
};
</script>

使用第三方库 vue-virtual-scroller 优化长列表

对于长列表,推荐使用 vue-virtual-scroller 实现高效滚动渲染,避免性能问题。

安装依赖:

vue实现滚动显示列表

npm install vue-virtual-scroller

示例代码:

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

<script>
import { RecycleScroller } from 'vue-virtual-scroller';
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css';

export default {
  components: { RecycleScroller },
  data() {
    return {
      list: Array.from({ length: 1000 }, (_, i) => ({
        id: i,
        text: `Item ${i + 1}`
      }))
    };
  }
};
</script>

<style>
.scroller {
  height: 300px;
}
.item {
  height: 50px;
  padding: 10px;
  border-bottom: 1px solid #eee;
}
</style>

监听滚动事件实现懒加载

通过监听滚动事件,在接近底部时动态加载更多数据,适合分页场景。

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

<script>
export default {
  data() {
    return {
      list: [],
      loading: false,
      page: 1
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.loading = true;
      // 模拟异步加载数据
      setTimeout(() => {
        const newItems = Array.from(
          { length: 20 }, 
          (_, i) => `Item ${this.list.length + i + 1}`
        );
        this.list = [...this.list, ...newItems];
        this.page++;
        this.loading = false;
      }, 1000);
    },
    handleScroll() {
      const container = this.$refs.scrollContainer;
      const threshold = 100;
      if (
        container.scrollTop + container.clientHeight >= 
        container.scrollHeight - threshold &&
        !this.loading
      ) {
        this.loadData();
      }
    }
  }
};
</script>

标签: 列表vue
分享给朋友:

相关文章

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mind…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

实现简易vue

实现简易vue

实现简易 Vue 的核心思路 Vue 的核心功能包括数据响应式、模板编译和虚拟 DOM。以下是一个简易实现方案,涵盖核心功能模块。 数据响应式系统 通过 Object.defineProperty…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…