当前位置:首页 > 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实现a

vue实现a

Vue 实现 A 功能的方法 在 Vue 中实现 A 功能可以通过多种方式,具体取决于 A 功能的具体需求。以下是几种常见的实现方法: 使用 Vue 组件 通过创建自定义组件来实现 A 功能。组件可…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <tem…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…

vue 实现vshow

vue 实现vshow

Vue 实现 v-show 在 Vue 中,v-show 是一个常用的指令,用于根据条件控制元素的显示或隐藏。与 v-if 不同,v-show 不会移除 DOM 元素,而是通过 CSS 的 displ…

vue实现监控

vue实现监控

Vue 实现监控功能的方法 在 Vue 中实现监控功能通常涉及数据变化监听、事件监听或第三方监控工具集成。以下是几种常见实现方式: 使用 Vue 的 watch 监听数据变化 通过 watch 可以…