美文网首页
element分页封装

element分页封装

作者: 蜗牛和曼巴 | 来源:发表于2019-05-09 18:28 被阅读0次

    可以完全复制粘贴直接用
    第一步在utils文件夹中创建一个scroll-to.js文件

    Math.easeInOutQuad = function (t, b, c, d) {
      t /= d / 2
      if (t < 1) {
        return c / 2 * t * t + b
      }
      t--
      return -c / 2 * (t * (t - 2) - 1) + b
    }
    
    // requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
    var requestAnimFrame = (function () {
      return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
        window.setTimeout(callback, 1000 / 60)
      }
    })()
    
    /**
     * Because it's so fucking difficult to detect the scrolling element, just move them all
     * @param {number} amount
     */
    function move(amount) {
      document.documentElement.scrollTop = amount
      document.body.parentNode.scrollTop = amount
      document.body.scrollTop = amount
    }
    
    function position() {
      return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
    }
    
    /**
     * @param {number} to
     * @param {number} duration
     * @param {Function} callback
     */
    export function scrollTo(to, duration, callback) {
      const start = position()
      const change = to - start
      const increment = 20
      let currentTime = 0
      duration = (typeof (duration) === 'undefined') ? 500 : duration
      var animateScroll = function () {
        // increment the time
        currentTime += increment
        // find the value with the quadratic in-out easing function
        var val = Math.easeInOutQuad(currentTime, start, change, duration)
        // move the document.body
        move(val)
        // do the animation unless its over
        if (currentTime < duration) {
          requestAnimFrame(animateScroll)
        } else {
          if (callback && typeof (callback) === 'function') {
            // the animation is done so lets callback
            callback()
          }
        }
      }
      animateScroll()
    }
    
    

    第二部,在公共文件夹components中创建Pagination文件夹index.vue

    <template>
      <div :class="{'hidden':hidden}" class="pagination-container">
        <el-pagination
          :background="background"
          :current-page.sync="currentPage"
          :page-size.sync="pageSize"
          :layout="layout"
          :page-sizes="pageSizes"
          :total="total"
          v-bind="$attrs"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </template>
    
    <script>
      import { scrollTo } from "@/utils/scroll-to";
    
      export default {
        name: "Pagination",
        props: {
          total: {
            required: true,
            type: Number
          },
          page: {
            type: Number,
            default: 1
          },
          limit: {
            type: Number,
            default: 20
          },
          pageSizes: {
            type: Array,
            default() {
              return [10, 20, 30, 50];
            }
          },
          layout: {
            type: String,
            default: "total, sizes, prev, pager, next, jumper"
          },
          background: {
            type: Boolean,
            default: true
          },
          autoScroll: {
            type: Boolean,
            default: true
          },
          hidden: {
            type: Boolean,
            default: false
          }
        },
        computed: {
          currentPage: {
            get() {
              return this.page;
            },
            set(val) {
              this.$emit("update:page", val);
            }
          },
          pageSize: {
            get() {
              return this.limit;
            },
            set(val) {
              this.$emit("update:limit", val);
            }
          }
        },
        methods: {
          handleSizeChange(val) {
            this.$emit("pagination", { page: this.currentPage, limit: val });
            if (this.autoScroll) {
              scrollTo(0, 800);
            }
          },
          handleCurrentChange(val) {
            this.$emit("pagination", { page: val, limit: this.pageSize });
            if (this.autoScroll) {
              scrollTo(0, 800);
            }
          }
        }
      };
    </script>
    
    <style scoped>
      .pagination-container {
        background: #fff;
        padding: 32px 16px;
      }
      .pagination-container.hidden {
        display: none;
      }
    </style>
    
    

    第三步,在需要的地方

    import Pagination from "@/components/Pagination";
     components: {
          Pagination
        },
    
     <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="listQuery.page"
            :limit.sync="listQuery.limit"
            @pagination="pageginaTo"
          />
    
     return {
      
           pagination: {
            page: 1, // 当前页
            count: 10, // 总条数
            pageSize: 10, // 一页显示多少条数据
            pageSizes: [10, 20, 30, 40], // 每页显示多少条
            sidx: '', // 排序列
            sord: '' // 排序类型
          },
          total: 1,
          listQuery: {
            page: 1,
            limit: 10
          },
          };
    
      // 分页
        pageginaTo(val) {
          this.listLoading = true
          this.pagination.pageSize = val.limit
          this.pagination.page = val.page
          this._ajax_rechargeOrderList()
        },
    

    相关文章

      网友评论

          本文标题:element分页封装

          本文链接:https://www.haomeiwen.com/subject/puzwoqtx.html