当前位置:首页 > VUE

vue实现上拉加载

2026-02-23 16:06:10VUE

Vue 实现上拉加载的方法

使用 Intersection Observer API

Intersection Observer API 提供了一种异步观察目标元素与祖先元素或顶级文档视窗交叉状态的方法。适合实现上拉加载。

<template>
  <div>
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
    <div ref="loadMore" class="load-more"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      page: 1,
      isLoading: false
    }
  },
  mounted() {
    this.initObserver()
    this.loadData()
  },
  methods: {
    initObserver() {
      const observer = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && !this.isLoading) {
          this.page++
          this.loadData()
        }
      })
      observer.observe(this.$refs.loadMore)
    },
    async loadData() {
      this.isLoading = true
      try {
        const newData = await fetchData(this.page)
        this.list = [...this.list, ...newData]
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

<style>
.load-more {
  height: 20px;
}
</style>

使用滚动事件监听

传统方法通过监听滚动事件判断是否到达底部。

<template>
  <div ref="container" @scroll="handleScroll">
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
    <div v-if="isLoading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      page: 1,
      isLoading: false
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    handleScroll() {
      const container = this.$refs.container
      const scrollHeight = container.scrollHeight
      const scrollTop = container.scrollTop
      const clientHeight = container.clientHeight

      if (scrollTop + clientHeight >= scrollHeight - 10 && !this.isLoading) {
        this.page++
        this.loadData()
      }
    },
    async loadData() {
      this.isLoading = true
      try {
        const newData = await fetchData(this.page)
        this.list = [...this.list, ...newData]
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

使用第三方库

可以使用现成的 Vue 插件如 vue-infinite-loading 简化实现。

安装:

npm install vue-infinite-loading

使用:

vue实现上拉加载

<template>
  <div>
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
    <InfiniteLoading @infinite="loadData" />
  </div>
</template>

<script>
import InfiniteLoading from 'vue-infinite-loading'

export default {
  components: { InfiniteLoading },
  data() {
    return {
      list: [],
      page: 1
    }
  },
  methods: {
    async loadData($state) {
      try {
        const newData = await fetchData(this.page)
        if (newData.length) {
          this.list = [...this.list, ...newData]
          this.page++
          $state.loaded()
        } else {
          $state.complete()
        }
      } catch (error) {
        $state.error()
      }
    }
  }
}
</script>

性能优化建议

  1. 节流处理滚动事件,避免频繁触发
  2. 添加加载状态提示,提升用户体验
  3. 在组件销毁时移除事件监听
  4. 考虑使用虚拟滚动处理大量数据

以上方法可根据具体需求选择实现,Intersection Observer API 是现代浏览器推荐的方式,而第三方库可以快速实现完整功能。

标签: 加载vue
分享给朋友:

相关文章

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-if…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue实现ssr

vue实现ssr

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

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…