当前位置:首页 > VUE

vue实现上拉分页

2026-01-22 03:43:01VUE

Vue 实现上拉分页的方法

使用第三方库(如 better-scroll)

安装 better-scroll 库:

vue实现上拉分页

npm install better-scroll --save

在 Vue 组件中引入并初始化:

vue实现上拉分页

import BScroll from 'better-scroll'

export default {
  data() {
    return {
      page: 1,
      pageSize: 10,
      list: [],
      loading: false,
      finished: false,
      scroll: null
    }
  },
  mounted() {
    this.initScroll()
    this.loadData()
  },
  methods: {
    initScroll() {
      this.scroll = new BScroll(this.$refs.wrapper, {
        pullUpLoad: true,
        click: true
      })
      this.scroll.on('pullingUp', this.loadMore)
    },
    loadData() {
      // 模拟异步请求
      setTimeout(() => {
        const newData = this.getMockData(this.page, this.pageSize)
        this.list = [...this.list, ...newData]
        this.loading = false
        this.scroll.finishPullUp()
        this.scroll.refresh()
      }, 500)
    },
    loadMore() {
      if (this.finished || this.loading) return
      this.loading = true
      this.page++
      this.loadData()
    },
    getMockData(page, size) {
      // 模拟数据
      return Array.from({length: size}, (_, i) => ({
        id: (page - 1) * size + i + 1,
        text: `Item ${(page - 1) * size + i + 1}`
      }))
    }
  }
}

使用原生滚动事件监听

export default {
  data() {
    return {
      page: 1,
      pageSize: 10,
      list: [],
      loading: false,
      finished: false
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
    this.loadData()
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
      const windowHeight = document.documentElement.clientHeight || document.body.clientHeight
      const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight

      if (scrollTop + windowHeight >= scrollHeight - 50 && !this.loading && !this.finished) {
        this.loadMore()
      }
    },
    loadData() {
      this.loading = true
      setTimeout(() => {
        const newData = this.getMockData(this.page, this.pageSize)
        this.list = [...this.list, ...newData]
        this.loading = false
      }, 500)
    },
    loadMore() {
      this.page++
      this.loadData()
    }
  }
}

使用 Vue 自定义指令

创建自定义指令:

Vue.directive('loadmore', {
  bind(el, binding) {
    const selectWrap = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
    selectWrap.addEventListener('scroll', function() {
      const condition = this.scrollHeight - this.scrollTop <= this.clientHeight
      if (condition) {
        binding.value()
      }
    })
  }
})

在组件中使用:

<template>
  <div v-loadmore="loadMore">
    <!-- 内容 -->
  </div>
</template>

注意事项

  • 需要处理加载状态,避免重复请求
  • 列表数据为空时显示提示信息
  • 滚动事件需要节流处理
  • 组件销毁时需要移除事件监听
  • 分页接口需要返回是否还有更多数据的标识

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

相关文章

vue实现选择季度

vue实现选择季度

Vue 实现选择季度的几种方法 使用下拉选择框(Select) 在 Vue 中可以通过 v-model 绑定一个下拉选择框来实现季度选择。数据可以预先定义为一个包含季度选项的数组。 <tem…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-li…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue实现钟表

vue实现钟表

使用 Vue 实现动态钟表 以下是一个基于 Vue 3 的动态钟表实现方案,包含时分秒指针和数字时间显示。 模板部分 <template> <div class="clock…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标…

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…