当前位置:首页 > 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 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…

vue实现多选

vue实现多选

Vue 实现多选功能 在 Vue 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

vue删除实现

vue删除实现

Vue 删除功能的实现方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 方法一:使用数组的 splice 方法 通过索引删除数组中的元素,适用于列表渲染的场景。 methods: {…

自己实现vue

自己实现vue

实现简易版 Vue.js 核心功能 要实现一个简易版 Vue.js,需要理解其核心功能:数据响应式、模板编译、依赖收集和虚拟 DOM。以下分模块实现关键功能。 数据响应式(Reactivity) 通…