vue3自定义分页器

代码逻辑

<div class="pagination">
    <div
      class="left"
      @click="perPage"
      :style="{ cursor: leftClickState ? 'no-drop' : '' }"
    >
      <i class="iconfont icon-a-xiangxia"></i>
    </div>
    <div
      class="number"
      v-for="(it, ix) in pageCollage"
      :key="'pagination' + ix"
      @click="currentPage(it)"
      :class="index == it ? 'bg' : ''"
    >
      {{ it == "left" || it == "right" ? "..." : it }}
    </div>
    <div
      class="right"
      @click="nextPage"
      :style="{ cursor: rightClickState ? 'no-drop' : '' }"
    >
      <i class="iconfont icon-a-xiangxia"></i>
    </div>
</div>

import { ref, computed, watch } from "vue";
let index = ref(1);

const props = defineProps({
  total: {
    type: Number,
    default: 10,
  },
});

const emit = defineEmits(["change"]);

//上一页的方法
function perPage() {
  if (index.value === 1) {
    index.value = 1;
  } else {
    index.value = index.value - 1;
  }
}
//下一页的方法
function nextPage() {
  if (index.value == props.total) {
    index.value = props.total;
  } else {
    index.value = index.value + 1;
  }
}
//点击具体的一页
function currentPage(page) {
  if (page === "left") {
    index.value = index.value - 4;
    if (index.value <= 0) index.value == 1;
  } else if (page == "right") {
    index.value = index.value + 4;
    if (index.value > props.total) index.value = props.total;
  } else {
    index.value = page;
  }
}

const pageCollage = computed(() => {
  let nowIndex = index.value;
  let allPages = props.total;

  if (allPages < 7) {
    return allPages;
  }

  if (nowIndex < 5) {
    return [1, 2, 3, 4, 5, "right", allPages];
  } else if (nowIndex > allPages - 4) {
    return [
      1,
      "left",
      allPages - 4,
      allPages - 3,
      allPages - 2,
      allPages - 1,
      allPages,
    ];
  } else {
    console.log('nowIndex',nowIndex)
    return [1, "left", nowIndex - 1, nowIndex, nowIndex + 1, "right", allPages];
  }
});

const leftClickState = computed(() => {
  if (index.value <= 1) return true;
  else return false;
});

const rightClickState = computed(() => {
  if (index.value >= props.total) return true;
  else return false;
});

watch(index, (newval, oldval) => {
  emit("change", newval);
});

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值