vue中移动端分页组件pagination

最近项目需要用到很简单的vue移动端分页组件,网上找了找,没有特别适合的,根据网上找来的代码,自己写了个简单的分页

根据设计稿,想要的就是最简单的,上一页,下一页,中间固定放三个显示页数的,不需要显示很多,也不需要省略号

原博客:https://blog.csdn.net/freya_yyy/article/details/81227016

效果图如下:

代码如下:

pagination.vue

<template>
  <div class="pagination">
    <ul>
      <li v-if="currentPage > 1">
        <span @click="prevPage">上一页</span>
      </li>
      <li v-if="currentPage == 1">
        <span class="disabled">上一页</span>
      </li>
      <li v-for="item in indexs" v-bind:class="{'active':currentPage==item}" class="item">
        <span @click="btnClick(item)">{{item}}</span>
      </li>
      <li v-if="currentPage!=allPage">
        <span @click="nextPage">下一页</span>
      </li>
      <li v-if="currentPage==allPage">
        <span class="disabled">下一页</span>
      </li>
      <li><span>共<i>{{allPage}}</i>页</span></li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'pagination',
  props: {
    currentPage: { // 当前第几页
      type: Number,
      default: 1
    },
    allPage: { // 总页数
      type: Number,
      default: 5
    },
    showIndex: { // 中间显示几个页数,要为奇数
      type: Number,
      default: 3
    }
  },
  methods: {
    nextPage () {
      this.$emit('setPage', this.currentPage + 1)
    },
    prevPage () {
      this.$emit('setPage', this.currentPage - 1)
    },
    btnClick (num) {
      if (num !== this.currentPage) {
        this.$emit('setPage', num)
      }
    }
  },
  computed: {
    indexs () {
      var left = 1
      var right = this.allPage
      var arr = []
      var index = parseInt(this.showIndex / 2)
      var middleIndex = index + 1
      if (this.allPage >= this.showIndex) {
        if (this.currentPage > middleIndex && this.currentPage < this.allPage - index) {
          left = this.currentPage - index
          right = this.currentPage + index
        } else if (this.currentPage <= middleIndex) {
          left = 1
          right = this.showIndex
        } else {
          left = this.allPage - (this.showIndex - 1)
          right = this.allPage
        }
      }
      while (left <= right) {
        arr.push(left)
        left++
      }
      return arr
    }
  }
}
</script>

<style scoped lang="less">
.pagination{
  ul{
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: .24rem;
    li{
      height: .6rem;
      margin: .12rem;
      span{
        display: block;
        padding: 0 .12rem;
        height: 100%;
        text-align: center;
        line-height: .6rem;
        color: #E0224F;
        border: 1px solid #E0224F;
        &.disabled{
          color: #D8D8D8;
          border: 1px solid #D8D8D8;
        }
      }
      &.item{
        min-width: .6rem;
        span{
          padding: 0;
          color: #413134;
          border: 1px solid #fff;
        }
        &.active{
          span{
            color: #E0224F;
            border: 1px solid #E0224F;
          }
        }
      }
    }
  }
}
</style>

使用:

<template>
   <pagination :allPage="10" @setPage="setPage" :currentPage="currentPage" :showIndex="3"></pagination>
</template>

<script>
import pagination from '@/components/pagination'

export default {
  data () {
    return {
      currentPage: 1,
    }
  },
  methods: {
     setPage (page) {
        this.currentPage = page
        console.log(this.currentPage)
     }
  }
}
</script>

  

  

 

转载于:https://www.cnblogs.com/Anne3/p/11354091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值