当前位置:首页 > VUE

vue实现上拉分页

2026-01-22 03:43:01VUE

Vue 实现上拉分页的方法

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

安装 better-scroll 库:

npm install better-scroll --save

在 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页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HT…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue拖动实现

vue拖动实现

实现 Vue 拖动功能的方法 使用 HTML5 拖放 API 在 Vue 中可以利用 HTML5 的原生拖放 API 实现拖动功能。通过 draggable 属性标记可拖动元素,结合 @dragsta…