<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>分页</title>
<style>
* {
padding: 0;
margin: 0;
}
.body {
width: 700px;
margin: 0 auto;
line-height: 24px;
}
.paginate {
width: 700px;
display: flex;
justify-content: center;
height: 38px;
line-height: 38px;
}
.paginate div {
height: 18px;
line-height: 18px;
padding: 10px;
margin: 0 10px;
border: 1px solid #eee;
}
.paginate .list {
height: 38px;
line-height: 38px;
border: none;
padding: 0;
display: flex;
}
.list p {
height: 18px;
line-height: 18px;
padding: 10px;
margin: 0 10px;
border: 1px solid #eee;
}
.active {
background-color: red;
}
.paginate .number {
width: 40px;
margin-right: 10px;
}
.paginate button {
width: 50px;
}
</style>
</head>
<body>
<div class="body">
</div>
<!-- 分页的容器 -->
<div class="paginate"></div>
<script src="page.js"></script>
<script>
let options = {
data: {
total: 105,
pageSize: 10
},
language: {
first: '<<',
prev: '<',
next: '>',
last: '>>'
},
};
let data = [];
for(let i = 0;i<105;i++){
data.push('这个是第'+(i+1)+'条数据');
}
new Page('.paginate', options, currentPage => {
let html = '';
let start = (currentPage - 1) * options.data.pageSize;
let end = currentPage * options.data.pageSize > options.data.total ? options.data.total : currentPage * options.data.pageSize;
for (let i = start; i < end; i++) {
html += `<div class="content">${data[i]}</div>`;
}
document.querySelector('.body').innerHTML = html;
});
</script>
</body>
</html>
function Page(elem, options, callback) {
this.callback = callback;
this.container = document.querySelector(elem);
this.listDom = null;
this.inputDom = null;
this.config = {
data: {
total: 300,
pageSize: 20
},
language: {
first: '首页',
prev: '上一页',
list: '',
next: '下一页',
last: '尾页'
}
}
this.mergeConfig(options);
this.totalPage = Math.ceil(this.config.data.total / this.config.data.pageSize);
this.currentPage = 1;
this.init();
this.executeEvent();
}
Page.prototype.init = function () {
this.container.innerHTML = '';
this.createOutsideTag();
this.createNumberTag();
this.creatJumpTag();
this.callback(this.currentPage);
}
Page.prototype.creatJumpTag = function () {
this.inputDom = document.createElement('input');
this.inputDom.style.width = '50px';
this.container.appendChild(this.inputDom);
let buttonDom = document.createElement('button');
buttonDom.innerHTML = 'Jump';
this.container.appendChild(buttonDom);
}
Page.prototype.executeEvent = function () {
this.container.addEventListener('click', event => {
switch (event.target.className) {
case 'first':
this.currentPage = 1;
this.init();
break;
case 'prev':
if (this.currentPage != 1) {
this.currentPage--;
this.init();
}
break;
case 'next':
if (this.currentPage < this.totalPage) {
this.currentPage++;
this.init();
}
break;
case 'last':
this.currentPage = this.totalPage;
this.init();
break;
default:
if (event.target.nodeName == 'P') {
this.currentPage = event.target.innerHTML - 0;
this.init();
} else if (event.target.nodeName.toLowerCase() == 'button') {
let value = Number(this.inputDom.value);
if (!isNaN(value) && value > 0 && value <= this.totalPage) {
this.currentPage = value;
this.init();
}
}
break;
}
})
}
Page.prototype.createNumberTag = function () {
if (this.totalPage <= 5) {
this.createNumber(1, 5);
return;
}
if (this.currentPage <= 3) {
this.createNumber(1, 5);
} else if (this.currentPage >= this.totalPage - 2) {
this.createNumber(this.totalPage - 4, this.totalPage);
} else {
this.createNumber(this.currentPage - 2, this.currentPage + 2);
}
}
Page.prototype.createNumber = function (start, end) {
for (let i = start; i <= end; i++) {
let pDom = document.createElement('p');
i == this.currentPage && pDom.setAttribute('class', 'active');
pDom.innerText = i;
this.listDom.appendChild(pDom);
}
}
Page.prototype.createOutsideTag = function () {
for (let key in this.config.language) {
let divDom = document.createElement('div');
divDom.className = key;
divDom.innerHTML = this.config.language[key];
key == 'list' && (this.listDom = divDom);
this.container.appendChild(divDom);
}
}
Page.prototype.mergeConfig = function (options) {
for (let attr in this.config) {
for (let key in this.config[attr]) {
options[attr] && options[attr][key] && (this.config[attr][key] = options[attr][key])
}
}
}