当前位置:首页 > VUE

vue实现上下翻页

2026-01-17 10:54:51VUE

vue实现上下翻页功能

使用v-for和数组切片

通过v-for渲染当前页数据,结合数组切片实现分页逻辑。data中定义当前页码和每页条数,计算属性返回当前页数据。

data() {
  return {
    list: [], // 全部数据
    currentPage: 1,
    pageSize: 10
  }
},
computed: {
  paginatedData() {
    const start = (this.currentPage - 1) * this.pageSize
    const end = start + this.pageSize
    return this.list.slice(start, end)
  }
},
methods: {
  prevPage() {
    if (this.currentPage > 1) this.currentPage--
  },
  nextPage() {
    if (this.currentPage < this.totalPages) this.currentPage++
  }
}

使用第三方分页组件

安装分页组件库如element-ui的Pagination组件,快速实现标准分页功能。

vue实现上下翻页

<template>
  <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    layout="prev, pager, next"
    :total="total">
  </el-pagination>
</template>

无限滚动加载

监听滚动事件,当滚动到底部时自动加载下一页数据,适合移动端场景。

vue实现上下翻页

mounted() {
  window.addEventListener('scroll', this.handleScroll)
},
methods: {
  handleScroll() {
    const scrollY = window.scrollY
    const visibleHeight = window.innerHeight
    const pageHeight = document.documentElement.scrollHeight
    const bottom = pageHeight - (scrollY + visibleHeight) < 50

    if (bottom && !this.loading) {
      this.loadNextPage()
    }
  }
}

路由参数分页

通过路由参数保存当前页码,实现可分享的分页链接。

watch: {
  '$route.query.page'(newPage) {
    this.currentPage = Number(newPage) || 1
  }
},
methods: {
  changePage(page) {
    this.$router.push({
      query: { ...this.$route.query, page }
    })
  }
}

键盘事件翻页

监听键盘上下箭头事件实现快捷翻页,提升用户体验。

mounted() {
  document.addEventListener('keydown', this.handleKeyDown)
},
methods: {
  handleKeyDown(e) {
    if (e.key === 'ArrowUp') this.prevPage()
    if (e.key === 'ArrowDown') this.nextPage()
  }
}

每种实现方式适用于不同场景,可根据具体需求选择合适方案。数组切片方案适合简单列表,第三方组件提供完整分页功能,无限滚动适合移动端浏览,路由参数分页支持页面状态共享,键盘事件增强操作便捷性。

标签: 翻页上下
分享给朋友:

相关文章

vue实现上下滚动

vue实现上下滚动

Vue 实现上下滚动效果 实现上下滚动效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 CSS 的 animation 和 @keyframes 实现简单的上下滚动效果。…

vue实现翻页动画

vue实现翻页动画

实现翻页动画的基本思路 在Vue中实现翻页动画通常需要结合CSS过渡或动画效果。可以通过Vue的过渡组件<transition>或<transition-group>来实现页面…

vue swiper 实现翻页

vue swiper 实现翻页

Vue 中使用 Swiper 实现翻页效果 安装 Swiper 依赖 确保项目已安装 swiper 和 vue-awesome-swiper(适用于 Vue 2/3): npm install s…

vue实现字体上下居中

vue实现字体上下居中

使用 flex 布局实现字体上下居中 在 Vue 中,可以通过 flex 布局快速实现文本的垂直居中。将父元素设置为 display: flex 并配合 align-items: center 即可。…

vue实现上下滚动列表

vue实现上下滚动列表

vue实现上下滚动列表 使用Vue实现上下滚动列表可以通过多种方式完成,包括CSS动画、JavaScript定时器或第三方库。以下是几种常见方法: 使用CSS动画实现滚动 通过CSS的@keyfra…

vue实现文字上下滚动

vue实现文字上下滚动

vue实现文字上下滚动 在Vue中实现文字上下滚动可以通过CSS动画或JavaScript动态控制样式来实现。以下是几种常见的方法: 使用CSS动画实现 通过CSS的@keyframes和trans…