原创 

nuxtjs 之vue分页插件轮子

分类:前端    455人阅读    IT小君  2020-08-21 13:05

已有vue 分页插件很多 怎奈 nuxtjs 引入一直不成功 ,没办法只能自己造个轮子,样式基于bootstrap

效果如图:

 点击最后两个页码或头部两个页码 ,页码会滚动

源码:

<template>
  <div>
    <div class="t1">
      {{total}}条 {{page}}页
      <select class="form-control" @change="changePageSize" v-model="pageSize">
        <option :value="item" :key="item" v-for="item in [1,2,3]">{{item}}/页</option>
      </select>
    </div>
    <div class="t2">
      <ul class="pagination">
        <li :class="{'disabled':index==1}">
          <a href="javascript:;" @click="home">
            <span aria-hidden="true">首页</span>
          </a>
        </li>
        <li :class="{'disabled':index==1}">
          <a href="javascript:;" @click="pre">
            <span aria-hidden="true">上一页</span>
          </a>
        </li>
        <template v-for="i in pageIndex">
          <li :key="i" :class="{'active':i==index}" v-if="i<=page">
            <a href="javascript:;" @click="pageChange(i)">{{i}}</a>
          </li>
        </template>
        <li>
          <a href="javascript:;" @click="nex" :class="{'disabled':index==page}">
            <span aria-hidden="true">下一页</span>
          </a>
        </li>
        <li>
          <a href="javascript:;" @click="tail" :class="{'disabled':index==page}">
            <span aria-hidden="true">尾页</span>
          </a>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import { mapMutations, mapGetters } from "vuex";
export default {
  props: ["total", "ipageSize"],
  data() {
    return {
      index: 1,
      currentIndex: 1,
      page: 0,
      pageSize: 1,
      pageIndex: [],
    };
  },
  mounted() {
    this.page = Math.ceil(this.total / this.pageSize);
    var max = 10;
    if (this.page < max) max = this.page;
    for (var i = 1; i <= max; i++) {
      this.pageIndex.push(i);
    }
  },
  methods: {
    pageChange(i) {
      this.index = i;
      if (this.currentIndex == this.index) return;
      this.currentIndex = this.index;
      if (
        (this.pageIndex[this.pageIndex.length - 1] == i ||
          this.pageIndex[this.pageIndex.length - 2] == i) &&
        this.pageIndex[this.pageIndex.length - 1] < this.page
      ) {
        i = this.pageIndex[this.pageIndex.length - 1];
        this.pageIndex.shift();
        this.pageIndex.shift();
        this.pageIndex.shift();
        this.pageIndex.shift();
        this.pageIndex.push(i + 1);
        this.pageIndex.push(i + 2);
        this.pageIndex.push(i + 3);
        this.pageIndex.push(i + 4);
      }

      this.offset = this.pageIndex[0] - 4;
      if (
        (this.pageIndex[1] == i || this.pageIndex[0] == i) &&
        this.offset > 0
      ) {
        i = this.pageIndex[0];
        this.pageIndex.pop();
        this.pageIndex.pop();
        this.pageIndex.pop();
        this.pageIndex.pop();
        this.pageIndex.unshift(i - 1);
        this.pageIndex.unshift(i - 2);
        this.pageIndex.unshift(i - 3);
        this.pageIndex.unshift(i - 4);
      }

      this.$emit("page", i, this.pageSize);
    },
    changePageSize() {
      this.page = Math.ceil(this.total / this.pageSize);
      var max = 10;
      this.pageIndex = [];
      for (var i = 1; i <= max; i++) {
        this.pageIndex.push(i);
      }
      if (this.index > this.page) this.index = this.page;
      this.$emit("page", this.index, this.pageSize);
    },
    home() {
      if (this.index == 1) return;
      this.index = 1;
      this.pageChange(this.index);
    },
    tail() {
      if (this.index == this.page) return;
      this.index = this.page;
      var max = 10;
      this.pageIndex = [];
      if (this.page < max) {
        for (var i = 1; (max = this.page), i <= max; i++) {
          this.pageIndex.push(i);
        }
      } else {
        for (var i = this.page - 10; (max = this.page), i <= max; i++) {
          this.pageIndex.push(i);
        }
      }
      this.pageChange(this.index);
    },
    pre() {
      if (this.index == 1) return;
      this.index--;
      this.pageChange(this.index);
    },
    nex() {
      if (this.index == this.page) return;
      this.index++;
      this.pageChange(this.index);
    },
  },
};
</script>
<style scoped>
* {
  font-size: 12.58px !important;
}
select {
  width: 90px;
  padding: 0px 5px 0px 15px;
  display: inline-block;
  border-radius: 3px;
  font-size: 13px;
  height: 24px;
  margin-left: 7px;
}
.pagination {
  margin: 0px;
}
.t1,
.t2 {
  display: inline-block;
}
.t1 {
  float: left;
  margin-top: 5px;
  margin-right: 10px;
  color: rgb(96, 98, 102);
}
</style>

使用:

import pageinate from '../../components/pageinate'
<pageinate :total="items.total"  @page="pageChange"></pageinate>


点击广告,支持我们为你提供更好的服务

响应式咖啡饮品宣传网站模板

小众时尚单品在线电子商务网站模板

html5 canvas彩色碎片组合球形旋转动画特效

HTML5现代家居装潢公司网站模板

css+js实现的颜色渐变数字时钟动画特效

html5图标下拉搜索框自动匹配代码

网页设计开发公司网站模板

canvas炫酷鼠标移动文字粒子特效

响应式时尚单品在线商城网站模板

响应式太阳能能源公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

html5 canvas进度条圆环图表统计动画特效

中小型创意设计服务公司网站模板

js+css3抽奖转盘旋转点餐代码

html5 svg夜空中星星流星动画场景特效

css鼠标跟随文字模糊特效

有机水果蔬菜HTML5网站模板

HTML5数字产品服务公司网站模板

现代时尚家具公司网站模板

jQuery右端悬浮带返回顶部特效

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务