当前位置:首页 > VUE

vue实现翻页效果

2026-03-28 03:40:13VUE

实现基础翻页功能

在Vue中实现翻页效果通常需要结合v-for指令和计算属性。以下是一个基础实现示例:

<template>
  <div>
    <ul>
      <li v-for="item in paginatedData" :key="item.id">{{ item.name }}</li>
    </ul>
    <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
    <span>第 {{ currentPage }} 页</span>
    <button @click="nextPage" :disabled="currentPage >= totalPages">下一页</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [...], // 你的数据数组
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.items.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  }
}
</script>

使用分页组件库

对于更复杂的需求,可以考虑使用现成的Vue分页组件:

  1. Element UI Pagination:

    <el-pagination
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="totalItems">
    </el-pagination>
  2. Vue Paginate:

    npm install vue-paginate
import VuePaginate from 'vue-paginate'
Vue.use(VuePaginate)
<paginate
  :page-count="totalPages"
  :click-handler="changePage"
  :prev-text="'上一页'"
  :next-text="'下一页'">
</paginate>

实现带页码跳转的分页

对于需要直接跳转到指定页码的场景:

<input 
  type="number" 
  v-model.number="jumpPage" 
  min="1" 
  :max="totalPages">
<button @click="goToPage">跳转</button>
methods: {
  goToPage() {
    if (this.jumpPage >= 1 && this.jumpPage <= this.totalPages) {
      this.currentPage = this.jumpPage
    }
  }
}

分页样式优化

为分页控件添加基本样式:

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.pagination button {
  margin: 0 5px;
  padding: 5px 10px;
  cursor: pointer;
}
.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

服务器端分页

当数据量很大时,需要实现服务器端分页:

methods: {
  async fetchData(page) {
    const response = await axios.get(`/api/items?page=${page}&size=${this.itemsPerPage}`)
    this.items = response.data.items
    this.totalItems = response.data.total
  },
  handlePageChange(page) {
    this.fetchData(page)
  }
}

分页与路由结合

将当前页码同步到URL路由:

watch: {
  currentPage(newVal) {
    this.$router.push({ query: { page: newVal } })
  }
},
created() {
  this.currentPage = parseInt(this.$route.query.page) || 1
}

响应式分页

根据屏幕尺寸调整每页显示数量:

vue实现翻页效果

data() {
  return {
    itemsPerPage: window.innerWidth < 768 ? 5 : 10
  }
},
created() {
  window.addEventListener('resize', this.handleResize)
},
methods: {
  handleResize() {
    this.itemsPerPage = window.innerWidth < 768 ? 5 : 10
  }
}

标签: 翻页效果
分享给朋友:

相关文章

vue实现效果

vue实现效果

Vue 实现常见效果的方法 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现表单元素与数据的双向绑定。例如输入框与数据的实时同步: <input v-model=…

vue效果实现

vue效果实现

Vue 效果实现方法 Vue.js 提供了多种方式实现动态效果,包括过渡动画、状态驱动的 UI 变化以及第三方动画库集成。以下是常见效果的实现方案: 过渡动画 使用 Vue 内置的 <tran…

vue滚动实现翻页

vue滚动实现翻页

实现滚动翻页的基本思路 在Vue中实现滚动翻页,通常需要监听滚动事件,判断是否滚动到页面底部,然后触发加载更多数据的操作。这可以通过结合原生DOM事件或第三方库来实现。 使用原生滚动事件监听 在Vu…

vue实现点选效果

vue实现点选效果

实现点选效果的基本思路 在Vue中实现点选效果通常涉及监听用户的点击事件,并通过动态绑定类名或样式来改变元素的外观。核心是利用Vue的响应式数据和事件绑定机制。 使用v-bind和v-on实现 通过…

react如何添加效果

react如何添加效果

React 添加动画效果的方法 使用 CSS 过渡动画 在 React 组件中直接通过 className 或 style 属性应用 CSS 过渡效果。定义 CSS 类包含 transition 属性…

vue实现翻牌效果

vue实现翻牌效果

实现翻牌效果的基本思路 翻牌效果通常指卡片正面和背面的翻转动画,可以通过CSS 3D变换和Vue的过渡系统实现。核心是利用transform-style: preserve-3d和rotateY旋转完…