function pagination(id, callback = null) { return { currentPage: 1, total: 1, paginationTemplate: $(id).html(), init: function() { this.initPagination() }, fetchHistory: function() { this.initPagination() }, getPaginationTemplate: function(index) { let li = document.createElement('a'); li.id = `pagination${index}`; li.text = index; if (index === "moreStart" || index === "moreEnd") { li.text = "..."; } li.onclick = function() { this.changePagination(index); callback && callback(); }.bind(this); li.setAttribute("href", 'javascript:void(0)'); li.setAttribute("class", 'yb-reward-history__pagination'); return li; }, //上一页 changePagination: function(index) { if (index === "1" && this.currentPage !== 1) { this.currentPage--; } else if (index === "1" && this.currentPage !== this.total) { this.currentPage++; } else if (index === "moreStart") { if (this.currentPage - 3 > 1) { this.currentPage -= 3; } } else if (index === "moreEnd") { if (this.currentPage + 3 < this.total) { if (this.currentPage === 1) { this.currentPage = 6; } else { this.currentPage += 3; } } } else if (parseInt(index)) { this.currentPage = index; } this.initPagination(); }, initPagination: function() { let element = document.querySelector(id); $(id).html(""); let fragment = document.createDocumentFragment(); let active = 'yb-reward-history__pagination yb-reward-history__pagination-active'; if (this.total > 1) { if (this.total > 5) { // let li = this.getPaginationTemplate("上一页"); // fragment.appendChild(li); if (this.currentPage <= 5) { for (let i = 1; i <= 5; i++) { let li = this.getPaginationTemplate(i); fragment.appendChild(li); if (i === this.currentPage) { li.setAttribute("class", active); } } } else { let li = this.getPaginationTemplate("moreStart"); fragment.appendChild(li); for (let i = this.currentPage - 2; i <= this.currentPage + 2 && i < this.total; i++) { let li = this.getPaginationTemplate(i); fragment.appendChild(li); if (i === this.currentPage) { li.setAttribute("class", active); } } } if (this.currentPage === 1 || this.currentPage + 3 !== this.total && this.currentPage + 2 !== this.total && this.currentPage + 1 !== this.total && this.currentPage !== this.total) { fragment.appendChild(this.getPaginationTemplate("moreEnd")); fragment.appendChild(this.getPaginationTemplate(this.total)); } else { let li = this.getPaginationTemplate(this.total); if (this.currentPage === this.total) { li.setAttribute("class", active) } fragment.appendChild(li); } // fragment.appendChild(this.getPaginationTemplate("下一页")); } else { for (let i = 1; i <= this.total; i++) { let li = this.getPaginationTemplate(i); fragment.appendChild(li); if (i === this.currentPage) { li.setAttribute("class", 'yb-reward-history__pagination yb-reward-history__pagination-active') } } } element.appendChild(fragment) } }, } }