当前位置:首页 > VUE

vue 实现滚动加载

2026-03-29 11:46:08VUE

实现滚动加载的基本原理

滚动加载的核心逻辑是监听滚动事件,当用户滚动到页面底部或指定位置时触发数据加载。Vue中可以通过v-infinite-scroll指令或手动监听滚动事件实现。

使用v-infinite-scroll指令

Element UI等库提供了现成的无限滚动指令。安装Element UI后可直接使用:

<template>
  <div v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="10">
    <div v-for="item in list" :key="item.id">{{ item.content }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      busy: false,
      page: 1
    }
  },
  methods: {
    loadMore() {
      this.busy = true
      fetchData(this.page).then(res => {
        this.list = this.list.concat(res.data)
        this.page++
        this.busy = false
      })
    }
  }
}
</script>

手动实现滚动监听

无第三方依赖的纯Vue实现方式:

<template>
  <div class="scroll-container" @scroll="handleScroll">
    <div v-for="item in items" :key="item.id">{{ item.text }}</div>
    <div v-if="loading">加载中...</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      loading: false,
      page: 1,
      hasMore: true
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    handleScroll(e) {
      const { scrollTop, clientHeight, scrollHeight } = e.target
      const bottomThreshold = 50 // 距底部50px触发

      if (scrollHeight - (scrollTop + clientHeight) < bottomThreshold 
          && !this.loading 
          && this.hasMore) {
        this.loadData()
      }
    },
    async loadData() {
      this.loading = true
      const newItems = await fetchData(this.page)
      this.items = [...this.items, ...newItems]
      this.page++
      this.hasMore = newItems.length > 0
      this.loading = false
    }
  }
}
</script>

<style>
.scroll-container {
  height: 500px;
  overflow-y: auto;
}
</style>

性能优化建议

添加防抖避免频繁触发滚动事件

import { debounce } from 'lodash-es'

methods: {
  handleScroll: debounce(function(e) {
    // 滚动逻辑
  }, 200)
}

使用Intersection Observer API实现更高效的检测

mounted() {
  const observer = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting && !this.loading && this.hasMore) {
      this.loadData()
    }
  })
  observer.observe(this.$refs.observerTarget)
}

注意事项

确保在组件销毁时移除事件监听

beforeDestroy() {
  window.removeEventListener('scroll', this.handleScroll)
}

分页加载时需要处理无更多数据的情况

vue 实现滚动加载

loadData() {
  if (!this.hasMore) return
  // ...加载逻辑
}

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

相关文章

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue 实现注册

vue 实现注册

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

vue实现hovuer

vue实现hovuer

Vue 实现 Hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 直接控制 通过 :hover 伪类实现,无需额外的 JavaScrip…