分页组件封装

在这里插入图片描述

scss

  /*分页*/
  .el-pagination {
    text-align: right;
    // margin-top: 20px;
  
    span.el-pagination__total {
      position: absolute;
      left: 0;
    }
  

  
    &.is-background {
      .el-pager {
        li {
          &:not(.disabled) {
            &.active {
              background: $button-danger
            }
  
            &:hover {
              // color: $default-bg-color
            }
          }
        }
      }
    }
  }
.page {
    text-align: center;

    .el-pagination__editor.el-input .el-input__inner {
        // height: 20px;
        border-radius: 4px !important;
    }

}

调用

import Pagination from '@/components/Pagination' //分页组件
components: {
	Pagination
},
<!-- 分页 -->
<div class="page" v-show="list.length">
	<Pagination
		:total="total"
		:page="page.page_number"
		v-on:pagination="getPage"
	></Pagination>
</div>

代码

<template>
	<div :class="{ hidden: hidden }" class="pagination-container">
		<div class="pagination-left">
			<!-- <span class="other-mes">每页显示</span> -->

			<span class="other-mes"
				>{{ page }}/{{ Math.ceil(total / pageSize) }},总共{{ total }}条数据</span
			>
		</div>
		<el-pagination
			small
			:background="background"
			:current-page.sync="currentPage"
			:page-size.sync="pageSize"
			:layout="layout"
			:total="total"
			v-bind="$attrs"
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
		/>
		<!-- 
			:page-sizes="pageSizes" -->

		<!-- <span class="other-mes">显示1-{{limit}}条,共{{total}}条记录,共{{Math.ceil(total/pageSize)}}</span> -->
	</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: 10
		},
		pageSizes: {
			type: Array,
			default() {
				return [10, 20, 50, 100]
			}
		},
		layout: {
			type: String,
			default: ' prev, pager, next, jumper, ->'
		},
		background: {
			type: Boolean,
			default: true
		},
		autoScroll: {
			type: Boolean,
			default: false
		},
		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)
			}
		}
	},
	mounted() {
		document.getElementsByClassName('el-pagination__jump')[0].childNodes[0].nodeValue = '跳至' //修改分页 前往 》跳至
	},
	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 lang="scss" scoped>
.pagination-left {
	display: flex;
	align-items: center;
}
.pagination-container {
	display: flex;
	align-items: center;
	justify-content: space-between;
	background: transparent;
	padding: 32px 16px;
}
.pagination-container.hidden {
	display: none;
}
.other-mes {
	font-size: 14px;
	color: #999999;
}
</style>

scrollTo.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
const 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
	const animateScroll = function () {
		// increment the time
		currentTime += increment
		// find the value with the quadratic in-out easing function
		const 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()
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值