当前位置:首页 > VUE

vue实现滑动加载

2026-03-08 19:32:09VUE

Vue 实现滑动加载的方法

使用 IntersectionObserver API

IntersectionObserver API 可以监听元素是否进入视口,适合实现滑动加载。

<template>
  <div>
    <div v-for="item in items" :key="item.id">{{ item.content }}</div>
    <div ref="loader" class="loader">Loading...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      isLoading: false
    }
  },
  mounted() {
    this.initObserver()
    this.loadItems()
  },
  methods: {
    initObserver() {
      const observer = new IntersectionObserver((entries) => {
        if (entries[0].isIntersecting && !this.isLoading) {
          this.loadItems()
        }
      })
      observer.observe(this.$refs.loader)
    },
    async loadItems() {
      this.isLoading = true
      const newItems = await fetch(`/api/items?page=${this.page}`).then(res => res.json())
      this.items = [...this.items, ...newItems]
      this.page++
      this.isLoading = false
    }
  }
}
</script>

使用窗口滚动事件

通过监听窗口滚动事件计算是否到达底部。

<template>
  <div>
    <div v-for="item in items" :key="item.id">{{ item.content }}</div>
    <div v-if="isLoading" class="loader">Loading...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      page: 1,
      isLoading: false
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
    this.loadItems()
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight
      const clientHeight = document.documentElement.clientHeight || window.innerHeight

      if (scrollTop + clientHeight >= scrollHeight - 100 && !this.isLoading) {
        this.loadItems()
      }
    },
    async loadItems() {
      this.isLoading = true
      const newItems = await fetch(`/api/items?page=${this.page}`).then(res => res.json())
      this.items = [...this.items, ...newItems]
      this.page++
      this.isLoading = false
    }
  }
}
</script>

使用第三方库

可以使用 vue-infinite-loading 等第三方库简化实现。

安装:

npm install vue-infinite-loading

使用:

vue实现滑动加载

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

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

export default {
  components: { InfiniteLoading },
  data() {
    return {
      items: [],
      page: 1
    }
  },
  methods: {
    async loadItems($state) {
      try {
        const newItems = await fetch(`/api/items?page=${this.page}`).then(res => res.json())
        if (newItems.length) {
          this.items = [...this.items, ...newItems]
          this.page++
          $state.loaded()
        } else {
          $state.complete()
        }
      } catch (error) {
        $state.error()
      }
    }
  }
}
</script>

性能优化建议

  • 使用防抖或节流控制滚动事件触发频率
  • 添加加载状态提示和错误处理
  • 在组件销毁时移除事件监听
  • 考虑使用虚拟滚动处理大量数据

以上方法可以根据项目需求选择适合的实现方式,IntersectionObserver API 是现代浏览器推荐的方法,而第三方库可以快速实现完整功能。

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

相关文章

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…