当前位置:首页 > VUE

vue无限滚动实现教程

2026-01-22 02:17:17VUE

Vue无限滚动实现方法

使用第三方库vue-infinite-loading

安装vue-infinite-loading库:

npm install vue-infinite-loading --save

在组件中引入并使用:

vue无限滚动实现教程

import InfiniteLoading from 'vue-infinite-loading';

export default {
  components: {
    InfiniteLoading
  },
  data() {
    return {
      items: [],
      page: 1
    };
  },
  methods: {
    loadMore($state) {
      axios.get(`/api/items?page=${this.page}`)
        .then(({ data }) => {
          if (data.length) {
            this.items.push(...data);
            this.page++;
            $state.loaded();
          } else {
            $state.complete();
          }
        });
    }
  }
}

模板部分:

<template>
  <div>
    <div v-for="item in items" :key="item.id">
      {{ item.content }}
    </div>
    <infinite-loading @infinite="loadMore"></infinite-loading>
  </div>
</template>

原生实现无限滚动

监听滚动事件实现:

vue无限滚动实现教程

export default {
  data() {
    return {
      items: [],
      isLoading: false,
      page: 1,
      hasMore: true
    };
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll);
    this.loadItems();
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll);
  },
  methods: {
    handleScroll() {
      const scrollY = window.scrollY;
      const visible = window.innerHeight;
      const pageHeight = document.documentElement.scrollHeight;
      const bottomOfPage = visible + scrollY >= pageHeight - 200;

      if (bottomOfPage && !this.isLoading && this.hasMore) {
        this.loadItems();
      }
    },
    loadItems() {
      this.isLoading = true;
      axios.get(`/api/items?page=${this.page}`)
        .then(({ data }) => {
          if (data.length) {
            this.items = [...this.items, ...data];
            this.page++;
          } else {
            this.hasMore = false;
          }
          this.isLoading = false;
        });
    }
  }
}

使用Intersection Observer API

现代浏览器支持的更高效方式:

export default {
  data() {
    return {
      items: [],
      observer: null,
      page: 1
    };
  },
  mounted() {
    this.observer = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        this.loadMore();
      }
    });

    this.observer.observe(this.$refs.loader);
    this.loadMore();
  },
  methods: {
    loadMore() {
      axios.get(`/api/items?page=${this.page}`)
        .then(({ data }) => {
          if (data.length) {
            this.items = [...this.items, ...data];
            this.page++;
          }
        });
    }
  }
}

模板中添加触发元素:

<div ref="loader"></div>

注意事项

  • 确保API接口支持分页查询
  • 处理加载状态和错误情况
  • 移动端需要考虑滚动性能优化
  • 大量数据渲染时使用虚拟滚动技术提高性能
  • 清除事件监听和观察器避免内存泄漏

标签: 教程vue
分享给朋友:

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现骰子

vue实现骰子

Vue 实现骰子功能 创建 Vue 组件 在 Vue 项目中创建一个骰子组件,例如 Dice.vue。该组件包含骰子的外观和逻辑。 <template> <div class=…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…