文件内容 page.min.js 8 KB 8338 字符
!function (a, b) { "use strict"; "object" == typeof module && "object" == typeof module.exports ? module.exports = a.document ? b(a, !0) : function (a) { if (!a.document) throw new Error("Page requires a window with a document"); return b(a) } : b(a) }("undefined" != typeof window ? window : this, function (a, b) { function c(a) { function b() { a.clickEvent && "function" == typeof a.clickEvent && a.clickEvent(c.currect, c) } a = a || {}, this.el = a.el, this.nums = a.nums, this.counts = a.counts || 10, this.parent = document.querySelector(this.el), this.parent.classList.add("page-elem-field-root"), this.parentNode = document.createElement("div"), this.parent.appendChild(this.parentNode), this.defaultPage = Number(a.defaultPage) || 1, this.jumpToOrder = !!a.jumpToOrder, this.showNowAndAll = !!a.showNowAndAll, this.showHeadFoot = !!a.showHeadFoot, this.head = a.head || "首页", this.foot = a.foot || "尾页", this.prev = "«", this.next = "»", this.headfoot = [], this.div = a.div || "div", this.domList = [], this.showDomList = []; var c = this; this.clickEvent = b, this.init() } return c.prototype = { init: function () { this.setPages(), this.createDom(), this.showDom(), this.watcherCurrect(), this.watcherPages(), this.watcherNums(), this.watcherCounts(), this.addDom(), this.reanderHeadFoot(), this.jumpToOrderPage(), this.showNowAndAllPage() }, setPages: function () { this.last = this.nums % this.counts, this.pages = parseInt(this.nums / this.counts), 0 != this.last && this.pages++ }, showNowAndAllPage: function () { if (this.showNowAndAll) { var a = document.createElement("div"), b = document.createElement("div"), c = document.createElement("div"), d = document.createElement("div"); a.classList.add("pagesbox"), b.classList.add("allPages"), c.classList.add("currectPage"), d.classList.add("line"), c.innerText = this.currect, b.innerText = this.pages, d.innerText = "/", a.appendChild(c), a.appendChild(d), a.appendChild(b), this.parent.appendChild(a), this.showNowAndAllPageDom = { pagesbox: a, allPages: b, currectPage: c, line: d } } }, jumpToOrderPage: function () { if (this.jumpToOrder) { var a = document.createElement("div"), b = document.createElement("div"), c = document.createElement("input"); a.classList.add("toPage"), b.classList.add("inputBox"), c.classList.add("inputborder"), c.type = "text", c.value = this.currect, this.addEventForInput(c), b.appendChild(c), a.appendChild(b), this.parent.appendChild(a), this.jumpToOrderPageDom = { toPage: a, input: c, inputBox: b } } }, addEventForInput: function (a, b) { var c = this; a.addEventListener("change", function (a) { var b = a.target.value; console.log(c.pages), b > c.pages ? b = c.pages : b < 1 && (b = 1), c.currect = b, this.value = b }) }, updateCurrectAndOrderBox: function (a) { this.jumpToOrder && (this.jumpToOrderPageDom.input.value = a), this.showNowAndAll && (this.showNowAndAllPageDom.currectPage.innerText = a) }, reanderHeadFoot: function () { if (this.showHeadFoot) { this.headfoot = []; var a = this.div, b = document.createElement(a), c = document.createElement(a); b.innerHTML = this.head, b.classList.add("item"), b.classList.add("head"), c.innerHTML = this.foot, c.classList.add("item"), c.classList.add("foot"), this.headfoot.push(b), this.headfoot.push(c), this.parentNode.insertBefore(b, this.parentNode.firstChild), this.parentNode.appendChild(c), this.addEventHeadFoot(), this.headFootDisable() } }, headFootDisable: function () { 1 === this.currect && this.headfoot[0].classList.add("item-disable"), this.currect === this.pages && this.headfoot[1].classList.add("item-disable") }, addEventHeadFoot: function () { var a = this; this.headfoot[0].addEventListener("click", function () { 1 != a.currect && (a.currect = 1) }), this.headfoot[1].addEventListener("click", function () { a.currect != a.pages && (console.log(1111), a.currect = a.pages) }) }, createDom: function () { var a = this.div, b = this.domList = [], c = document.createElement(a), d = document.createElement(a); c.innerHTML = this.prev, c.classList.add("item"), d.innerHTML = this.next, d.classList.add("item"), b[0] = c; for (var e = this.pages, f = 0; f < e; f++) { var g = document.createElement(a); g.classList.add("item"), g.innerHTML = f + 1, b[f + 1] = g } return b.push(d), b }, showDom: function () { var a = this.domList, b = a.length, c = this.showDomList = [], d = this.defaultPage; if (b <= 12) for (var e = 0; e < b; e++)c[e] = a[e]; else { var f = d - 6 < 0 ? 0 : d - 6; this.pages - d < 5 && (f = this.pages - 10), c[0] = a[0]; for (var e = 1; e < 11; e++)c[e] = a[e + f]; c[e] = a[b - 1] } return c }, addDom: function () { this.parentNode.innerHTML = "", this.parentNode.classList.add("page-elem-field"), this.addAndRemoveClass(), this.addEvent(); for (var a = document.createDocumentFragment(), b = this.showDomList, c = b.length, d = 0; d < c; d++)a.appendChild(b[d]); this.parentNode.appendChild(a) }, addEvent: function () { for (var a = this.domList, b = a.length, c = 0; c < b; c++)a[c].addEventListener("click", this.jump.bind(a[c], this)) }, jump: function (a) { var b = this.innerHTML; b == a.prev && 1 == a.currect || b == a.next && a.currect == a.pages || b == a.currect || (b == a.prev && a.currect > 1 ? a.currect-- : b == a.next && a.currect < a.pages ? a.currect++ : b != a.prev && b != a.next && (a.currect = Number(b))) }, addAndRemoveClass: function () { var a = this.domList; 1 === this.currect && a[0].classList.add("item-disable"), this.currect === this.pages && a[a.length - 1].classList.add("item-disable"), a[this.currect].classList.add("active") }, activeCurrectItem: function (a) { var b = this.domList; b[a].classList.add("active"), b[0].classList.remove("item-disable"), b[b.length - 1].classList.remove("item-disable"), this.showHeadFoot && (this.headfoot[0].classList.remove("item-disable"), this.headfoot[1].classList.remove("item-disable")), 1 == a && (b[0].classList.add("item-disable"), this.showHeadFoot && this.headfoot[0].classList.add("item-disable")), a == this.pages && (b[b.length - 1].classList.add("item-disable"), this.showHeadFoot && this.headfoot[1].classList.add("item-disable")) }, moveDom: function (a, b) { var c = this.domList, d = this.showDomList, e = a - b, f = 0; if (e > 5) { f = e - 5; var g = Number(d[d.length - 2].innerHTML), h = this.pages - g; if (h > 0 && f > 0) { for (var i = Math.min(h, f), j = 0; j < i; j++)d.splice(d.length - 1, 0, c[g + j + 1]), this.parentNode.insertBefore(c[g + j + 1], d[d.length - 1]), this.parentNode.removeChild(d[j + 1]); d.splice(1, i) } } if (e <= 5 && 1 != b) { f = a <= 5 ? b - 1 : 5 - e; for (var i = f, j = 0; j < i; j++)this.parentNode.insertBefore(c[b - j - 1], d[1]), this.parentNode.removeChild(d[d.length - 2 - j]), d.splice(1, 0, c[b - j - 1]); d.splice(d.length - 1 - i, i) } return b = Number(d[1].innerHTML) }, watcherNums: function () { var a = this.nums; Object.defineProperty(this, "nums", { enumerable: !0, configrable: !1, set: function (b) { if ("number" != typeof b || isNaN(b)) return console.error("不是数字"); b <= 0 || a === b || (a = b, this.setPages()) }, get: function () { return a } }) }, watcherCounts: function () { var a = this.counts; Object.defineProperty(this, "counts", { enumerable: !0, configrable: !1, set: function (b) { if ("number" != typeof b || isNaN(b)) return console.error("不是数字"); b <= 0 || a === b || (a = b, this.setPages()) }, get: function () { return a } }) }, watcherPages: function () { var a = this.pages; Object.defineProperty(this, "pages", { enumerable: !0, configrable: !1, set: function (b) { if ("number" != typeof b || isNaN(b)) return console.error("不是数字"); b <= 0 || a === b || (a = b, this.currect > a && (this.currect = a), this.createDom(), this.showDom(), this.addDom(), this.reanderHeadFoot(), this.showNowAndAllPageDom.allPages.innerText = a) }, get: function () { return a } }) }, watcherCurrect: function () { var a = this.defaultPage, b = this.domList, c = this.showDomList, d = Number(c[1].innerHTML); Object.defineProperty(this, "currect", { enumerable: !0, configrable: !1, set: function (c) { b = this.domList, b[a].classList.remove("active"), a = c, this.clickEvent(a, this), this.updateCurrectAndOrderBox(a), d = this.moveDom(a, d), this.activeCurrectItem(a) }, get: function () { return a } }) } }, c.prototype.constructor = c, void 0 === b && (a.Page = c), c });
​ ​