原生js实现分页逻辑
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript原生手动分页组件</title>
<style>
body, div, ul, ol, li, p, img, a, span{ margin:0;padding:0;}
#pagenation { text-align: center;margin: 20px;}
#pagenation [class$=_pager]{ user-select: none;list-style: none;display: inline-block;vertical-align: top;font-size: 0;padding: 0;margin: 0;}
#pagenation button, #pagenation li {
display: inline-block;vertical-align: top;font-size: 13px;box-sizing: border-box;text-align: center;cursor: pointer;padding: 0 4px;height: 28px;line-height: 28px;
border: none;margin: 0 5px;background-color: #f4f4f5;color: #606266;min-width: 30px;border-radius: 2px;
}
#pagenation button:disabled { color: #c0c4cc;cursor: not-allowed;}
#pagenation li:hover { color: #409EFF;}
#pagenation .number.active { background-color: #409EFF;color: #fff;cursor: default;}
#pagenation li.btn-quicknext, #pagenation li.btn-quickprev { font-weight: bold;line-height: 20px;}
#pagenation li.btn-quicknext.active, #pagenation li.btn-quickprev.active { font-size: 12px;letter-spacing: -2px;line-height: 26px;}
#other { text-align: center;margin: 20px;}
#other [class$=_pager]{ user-select: none;list-style: none;display: inline-block;vertical-align: top;font-size: 0;padding: 0;margin: 0;}
#other button, #other li {
display: inline-block;vertical-align: top;font-size: 13px;box-sizing: border-box;text-align: center;cursor: pointer;padding: 0 4px;height: 28px;line-height: 28px;
border: none;margin: 0 5px;background-color: #f4f4f5;color: #606266;min-width: 30px;border-radius: 2px;
}
#other button:disabled { color: #c0c4cc;cursor: not-allowed;}
#other li:hover { color: #409EFF;}
#other .number.active { background-color: #409EFF;color: #fff;cursor: default;}
#other li.btn-quicknext, #other li.btn-quickprev { font-weight: bold;line-height: 20px;}
#other li.btn-quicknext.active, #other li.btn-quickprev.active { font-size: 12px;letter-spacing: -2px;line-height: 26px;}
</style>
</head>
<body>
<div id="pagenation" class="pagination"></div>
<hr>
<div id=other class="pagination"></div>
</body>
<script type="text/javascript">
function Pagenation(obj) {
this.wrap = obj.wrap;
this.page = obj.page || 1;
this.size = obj.size || 10;
this.total = obj.total || 0;
this.pages = Math.ceil(this.total / this.size);
this.callback = obj.callback;
this.quickPages = 5;
this.interval = obj.interval ? (obj.interval < 3 ? obj.interval : 4) : 4;
this.init();
}
Pagenation.prototype = {
constructor: Pagenation,
init: function() {
this.wrap.innerHTML = '';
this.wrap.appendChild(this.prevBtn());
if (this.pages > 0) {
this.wrap.appendChild(this.pageNum());
}
this.wrap.appendChild(this.nextBtn());
this.switchPage();
},
changeTotal: function (t) {
this.total = t || 0;
this.pages = Math.ceil(this.total / this.size);
this.init();
},
switchPage: function() {
var _this = this;
let numbers = this.wrap.querySelectorAll('.number');
for (let i = 0, len = numbers.length; i < len; i++) {
numbers[i].onclick = function () {
if (_this.page != this.innerText) {
console.log(_this.page, this.innerText)
_this.page = Number(this.innerText);
_this.init();
_this.callback(_this.page);
}
};
}
this.wrap.querySelector('.btn-prev').onclick = function() {
_this.page = --_this.page;
_this.init();
_this.callback(_this.page);
};
this.wrap.querySelector('.btn-next').onclick = function() {
_this.page = ++_this.page;
_this.init();
_this.callback(_this.page);
};
let quickprev = this.wrap.querySelector('.btn-quickprev')
if (quickprev) {
quickprev.onclick = function() {
_this.page = _this.page - _this.quickPages < 0 ? 1 : _this.page - _this.quickPages;
_this.init();
_this.callback(_this.page);
};
quickprev.onmouseenter = function(e) {
e.target.classList.add('active');
e.target.innerText = '...';
};
quickprev.onmouseleave = function(e) {
e.target.classList.remove('active');
e.target.innerText = '...';
};
}
let quicknext = this.wrap.querySelector('.btn-quicknext')
if (quicknext) {
quicknext.onclick = function() {
_this.page = _this.page + _this.quickPages > _this.pageT ? _this.pageT : _this.page + _this.quickPages;
_this.init();
_this.callback(_this.page);
};
quicknext.onmouseenter = function(e) {
e.target.classList.add('active');
e.target.innerText = '>>';
};
quicknext.onmouseleave = function(e) {
e.target.classList.remove('active');
e.target.innerText = '...';
};
}
},
prevBtn: function() {
let btnprev = document.createElement('button');
btnprev.setAttribute('class', 'btn-prev');
btnprev.setAttribute('type', 'button');
btnprev.innerText = '<';
if (this.page === 1 || this.pages <= 0) {
btnprev.setAttribute('disabled', true);
} else {
btnprev.removeAttribute('disabled');
}
return btnprev;
},
nextBtn: function() {
let btnnext = document.createElement('button');
btnnext.setAttribute('class', 'btn-next');
btnnext.setAttribute('type', 'button');
btnnext.innerText = '>';
if (this.page === this.pages || this.pages <= 0) {
btnnext.setAttribute('disabled', true);
} else {
btnnext.removeAttribute('disabled');
}
return btnnext;
},
pageNum: function() {
let ol = document.createElement('ol');
ol.classList.add(this.wrap.id + '_pager');
let currentPage = this.page;
let resetPages = this.pages;
let first = currentPage <= this.interval ? 1 : currentPage > (resetPages - this.interval) ? resetPages - (this.interval + 1) : currentPage - (this.interval - 1);
first = first === 0 ? 1 : first;
ol.appendChild(this.renderNum(1));
if (first - 1 >= 1){
ol.appendChild(this.renderButton('btn-quickprev'));
}
let last = first + this.interval + 2;
for (let i = first + 1; i < last; i++) {
if (i === resetPages - this.interval && last > resetPages) {
ol.appendChild(this.renderNum(first));
}
if (i <= resetPages) {
ol.appendChild(this.renderNum(i));
}
if (i === last -1 && resetPages - i > 1) {
ol.appendChild(this.renderButton('btn-quicknext'));
ol.appendChild(this.renderNum(resetPages));
}
}
return ol;
},
renderNum: function(i) {
let number = document.createElement('li');
number.classList.add('number');
number.innerText = i;
if (i === this.page) {
number.classList.add('active');
}
return number;
},
renderButton: function(name) {
let button = document.createElement('li');
button.classList.add(name);
button.innerText = '...';
return button;
}
}
</script>
<script type="text/javascript">
var pagenation = new Pagenation({
wrap: document.getElementById('pagenation'),
page: 1,
size: 1,
total:100,
interval:1,
callback: function(page) {
}
});
var other = new Pagenation({
wrap: document.getElementById('other'),
page: 1,
size: 1,
total:100,
interval:1,
callback: function(page) {
}
})
</script>
</html>