vue之 – 分页器封装
使用组件
<template>
<pagination
:total="listQuery.total"
:page.sync="listQuery.pageNum"
:limit.sync="listQuery.pageSize"
@pagination="getList"
/>
</template>
import Pagination from "@/components/Pagination/index"
data(){
listQuery: {
current: 1,
pageSize: 10,
total: 100,
filter: {}
}
},
methods:{
getList(){
//接口调用
}
}
组件内容
<template>
<div :class="{ hidden: hidden }"
class="pagination-container">
<!-- :hide-on-single-page="total < pageSize ? true : false"-->
<!-- :hide-on-single-page="total < 10 ? true : false"-->
<el-pagination :hide-on-single-page="total < 1 ? true : false"
:background="background"
:current-page.sync="currentPage"
:layout="layout"
:page-sizes="pageSizes"
:page-size.sync="pageSize"
:total="total"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange" />
</div>
</template>
<script>
import { scrollTo } from '@/libs/util.scrollto'
export default {
name: 'Pagination',
props: {
total: {
required: true,
type: Number,
},
page: {
type: Number,
default: 1,
},
limit: {
// pageSze
type: Number,
default: 20,
},
pageSizes: {
type: Array,
default() {
return [10, 20, 30, 50]
},
},
layout: {
type: String,
default: 'total,jumper, prev, pager, next,sizes',
},
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 {
padding: 24px 16px;
display: flex;
justify-content: flex-start;
}
.pagination-container.hidden {
display: none;
}
</style>
<style lang="scss">
// cover element-ui css
.pagination-container {
.btn-prev,
.btn-next {
width: 28px;
height: 28px;
background: #fff !important;
color: #b7b7b7;
border: 1px solid #dfdfdf;
}
.el-pagination.is-background .el-pager li {
background-color: #fff;
border: 1px solid #dfdfdf;
}
.number {
width: 28x;
height: 28px !important;
line-height: 28px;
}
.el-pagination__editor.el-input .el-input__inner {
height: 28x;
}
.el-pagination .el-select .el-input .el-input__inner {
height: 28px;
}
}
.el-pagination__total {
line-height: 28px !important;
}
</style>
util组件 => scrollto
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()
}