当前位置:首页 > VUE

vue实现滚动分页

2026-01-17 07:43:38VUE

实现滚动分页的基本思路

滚动分页(Infinite Scroll)是一种常见的前端分页加载方式,当用户滚动到页面底部时自动加载下一页数据。Vue 结合现代前端工具可以轻松实现这一功能。

监听滚动事件

通过监听窗口的滚动事件,判断是否滚动到底部。可以使用 window.addEventListener 或 Vue 的自定义指令。

mounted() {
  window.addEventListener('scroll', this.handleScroll);
},
beforeDestroy() {
  window.removeEventListener('scroll', this.handleScroll);
},
methods: {
  handleScroll() {
    const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    const windowHeight = window.innerHeight;
    const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    if (scrollTop + windowHeight >= scrollHeight - 100) {
      this.loadMore();
    }
  },
  loadMore() {
    if (this.loading || !this.hasMore) return;
    this.loading = true;
    this.fetchData(this.currentPage + 1);
  }
}

使用 Intersection Observer API

Intersection Observer 是一种更高效的滚动监听方式,适合现代浏览器。

data() {
  return {
    observer: null,
    loading: false,
    hasMore: true
  };
},
mounted() {
  this.observer = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting && !this.loading && this.hasMore) {
      this.loadMore();
    }
  });
  this.observer.observe(document.querySelector('#load-more-trigger'));
},
beforeDestroy() {
  this.observer.disconnect();
},
methods: {
  loadMore() {
    this.loading = true;
    this.fetchData(this.currentPage + 1);
  }
}

数据加载与分页逻辑

在 Vue 组件中管理分页状态和数据加载。

vue实现滚动分页

data() {
  return {
    items: [],
    currentPage: 1,
    totalPages: 1,
    loading: false
  };
},
methods: {
  async fetchData(page) {
    if (this.loading) return;
    this.loading = true;
    try {
      const response = await api.fetchItems(page);
      this.items = [...this.items, ...response.data];
      this.currentPage = page;
      this.totalPages = response.totalPages;
    } catch (error) {
      console.error(error);
    } finally {
      this.loading = false;
    }
  }
}

使用第三方库

如果需要更高级的功能,可以考虑使用第三方库如 vue-infinite-loading

安装:

vue实现滚动分页

npm install vue-infinite-loading

使用:

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

<script>
import InfiniteLoading from 'vue-infinite-loading';
export default {
  components: { InfiniteLoading },
  methods: {
    async loadMore($state) {
      try {
        const response = await api.fetchItems(this.currentPage + 1);
        this.items.push(...response.data);
        this.currentPage++;
        if (this.currentPage >= response.totalPages) {
          $state.complete();
        } else {
          $state.loaded();
        }
      } catch (error) {
        $state.error();
      }
    }
  }
};
</script>

优化与注意事项

避免频繁触发滚动事件,可以通过防抖(debounce)优化性能。

methods: {
  handleScroll: _.debounce(function() {
    const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    const windowHeight = window.innerHeight;
    const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    if (scrollTop + windowHeight >= scrollHeight - 100) {
      this.loadMore();
    }
  }, 200)
}

确保在组件销毁时移除事件监听或断开观察器,避免内存泄漏。

beforeDestroy() {
  window.removeEventListener('scroll', this.handleScroll);
  if (this.observer) this.observer.disconnect();
}

标签: 分页vue
分享给朋友:

相关文章

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…

vue 实现注册

vue 实现注册

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

vue 实现上传

vue 实现上传

Vue 实现文件上传 使用 Vue 实现文件上传可以通过原生 HTML 的 <input type="file"> 或第三方库(如 axios、vue-upload-component)完…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…