pageination.ie.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. function _instanceof(left, right) {
  2. if (
  3. right != null &&
  4. typeof Symbol !== "undefined" &&
  5. right[Symbol.hasInstance]
  6. ) {
  7. return right[Symbol.hasInstance](left);
  8. } else {
  9. return left instanceof right;
  10. }
  11. }
  12. function _classCallCheck(instance, Constructor) {
  13. if (!_instanceof(instance, Constructor)) {
  14. throw new TypeError("Cannot call a class as a function");
  15. }
  16. }
  17. function _defineProperties(target, props) {
  18. for (var i = 0; i < props.length; i++) {
  19. var descriptor = props[i];
  20. descriptor.enumerable = descriptor.enumerable || false;
  21. descriptor.configurable = true;
  22. if ("value" in descriptor) descriptor.writable = true;
  23. Object.defineProperty(target, descriptor.key, descriptor);
  24. }
  25. }
  26. function _createClass(Constructor, protoProps, staticProps) {
  27. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  28. if (staticProps) _defineProperties(Constructor, staticProps);
  29. return Constructor;
  30. }
  31. var Page =
  32. /*#__PURE__*/
  33. (function() {
  34. "use strict";
  35. function Page() {
  36. var _ref =
  37. arguments.length > 0 && arguments[0] !== undefined
  38. ? arguments[0]
  39. : {},
  40. _ref$el = _ref.el,
  41. el = _ref$el === void 0 ? ".pagination-box" : _ref$el,
  42. _ref$now = _ref.now,
  43. now = _ref$now === void 0 ? 1 : _ref$now,
  44. _ref$show = _ref.show,
  45. show = _ref$show === void 0 ? 5 : _ref$show,
  46. _ref$total = _ref.total,
  47. total = _ref$total === void 0 ? 1000 : _ref$total,
  48. _ref$size = _ref.size,
  49. size = _ref$size === void 0 ? 10 : _ref$size,
  50. _ref$click = _ref.click,
  51. click = _ref$click === void 0 ? null : _ref$click;
  52. _classCallCheck(this, Page);
  53. this.now = now;
  54. this.show = show;
  55. this.total = total;
  56. this.size = size;
  57. this.min = 1;
  58. this.max = Math.ceil(total / size);
  59. this.contentArr = [];
  60. this.contentDom = [];
  61. this.dom = document.querySelector(el);
  62. this.abDom = [];
  63. this.otherDom = [];
  64. this.click = click;
  65. }
  66. _createClass(Page, [
  67. {
  68. key: "init",
  69. value: function init() {
  70. var _this = this;
  71. // dom数组
  72. var cArr = ["back", "prev", "next", "cont", "min", "max"];
  73. cArr.forEach(function(v) {
  74. _this.otherDom.push(
  75. _this.dom.querySelector('button[name="'.concat(v, '"]'))
  76. );
  77. });
  78. this.otherDom[4].innerText = this.min;
  79. this.otherDom[5].innerText = this.max;
  80. if (this.min === this.max) {
  81. this.otherDom[5].style.display = "none";
  82. } // 创建dom
  83. this.getContent(); // 点击事件
  84. $(this.dom).off("click").on("click", function(e) {
  85. if (e.target.nodeName === "BUTTON") {
  86. var type = e.target.getAttribute("name");
  87. var tempNumber = 0;
  88. switch (type) {
  89. case "back": {
  90. tempNumber = -1;
  91. break;
  92. }
  93. case "cont": {
  94. tempNumber = 1;
  95. break;
  96. }
  97. case "prev": {
  98. tempNumber = -_this.show;
  99. break;
  100. }
  101. case "next": {
  102. tempNumber = _this.show;
  103. break;
  104. }
  105. default: {
  106. tempNumber = parseInt(e.target.innerText) - _this.now;
  107. }
  108. }
  109. var tempNow = _this.now + tempNumber;
  110. if (_this.min <= tempNow && tempNow <= _this.max) {
  111. _this.now = tempNow;
  112. }
  113. typeof _this.click === "function"
  114. ? _this.click(_this.now, type)
  115. : null;
  116. _this.getContent();
  117. _this.upData();
  118. }
  119. });
  120. var nextButton = this.otherDom[2];
  121. this.abDom = this.otherDom.slice(-2);
  122. this.contentArr.forEach(function(v) {
  123. var b = document.createElement("button");
  124. b.innerText = v;
  125. _this.contentDom.push(b);
  126. _this.dom.insertBefore(b, nextButton);
  127. });
  128. this.upData();
  129. }
  130. },
  131. {
  132. key: "upData",
  133. value: function upData() {
  134. var _this2 = this;
  135. this.abDom.forEach(function(v) {
  136. return (v.className = "");
  137. });
  138. this.otherDom[0].className = "";
  139. this.otherDom[3].className = "";
  140. if (this.now <= this.min) {
  141. this.now = this.min;
  142. this.abDom[0].className = "now";
  143. this.otherDom[0].className = "stop";
  144. }
  145. if (this.now >= this.max) {
  146. this.now = this.max;
  147. this.abDom[1].className = "now";
  148. this.otherDom[3].className = "stop";
  149. }
  150. this.contentDom.forEach(function(v, i) {
  151. v.className = "";
  152. var n = _this2.contentArr[i];
  153. if (n === _this2.now) {
  154. v.className = "now";
  155. }
  156. v.innerText = n;
  157. });
  158. }
  159. },
  160. {
  161. key: "getContent",
  162. value: function getContent() {
  163. var _this3 = this;
  164. var n = this.now;
  165. var size = this.show;
  166. var arr = [];
  167. for (var i = 0; i < size; i++) {
  168. arr[i] = i;
  169. } // default
  170. var content = Math.floor(arr.length / 2);
  171. var show = [true, true];
  172. var mapF = function mapF(v, i) {
  173. var step = content - i;
  174. return n - step;
  175. }; // min
  176. if (this.max - size < n) {
  177. mapF = function mapF(v, i) {
  178. return _this3.max - size + i;
  179. };
  180. show[1] = false;
  181. }
  182. if(n+(size-1)/2<this.max-1){
  183. show[1] = true;
  184. }
  185. if (n <= size - this.min) {
  186. mapF = function mapF(v, i) {
  187. return _this3.min + 1 + i;
  188. };
  189. show[0] = false;
  190. } // max
  191. if (this.max - this.min <= this.show) {
  192. show = [false, false];
  193. }
  194. this.contentArr = arr.map(mapF).filter(function(v) {
  195. return v > _this3.min && v < _this3.max;
  196. });
  197. if(this.min+1==this.contentArr[0]){
  198. show[0] = false;
  199. }
  200. if(this.max-1==this.contentArr[this.contentArr.length-1]){
  201. show[1] = false;
  202. }
  203. show.forEach(function(v, i) {
  204. _this3.otherDom[i + 1].style.display = v ? "inline-block" : "none";
  205. _this3.otherDom[i + 1].style.pointerEvents = "none";
  206. });
  207. }
  208. }
  209. ]);
  210. return Page;
  211. })();