当前位置:首页 > 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 实现多级表头的方法 使用 el-table 组件 Element UI 的 el-table 组件支持多级表头的实现,通过嵌套 el-table-column 即可完成。 <el-ta…

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue实现飘窗原理

vue实现飘窗原理

vue实现飘窗原理 飘窗效果通常指在页面上悬浮显示的弹窗或通知,跟随页面滚动或固定在某个位置。Vue实现这一效果主要依赖组件化开发、CSS定位和动态数据绑定。 组件结构设计 创建一个独立的Vue组…