xlPaging.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;(function ($, window, document, undefined) {
  2. 'use strict';
  3. function Paging(element, options) {
  4. this.element = element;
  5. this.options = {
  6. nowPage: options.nowPage || 1, // 当前页码
  7. pageNum: options.pageNum, // 总页码
  8. buttonNum: (options.buttonNum>=5?options.buttonNum:5) || 7,// 页面显示页码数量
  9. callback: options.callback // 回调函数
  10. };
  11. this.init();
  12. }
  13. Paging.prototype = {
  14. constructor : Paging,
  15. init : function() {
  16. this.createHtml();
  17. this.bindClickEvent();
  18. this.disabled();
  19. },
  20. createHtml : function(){
  21. var me = this;
  22. var nowPage = this.options.nowPage;
  23. var pageNum = this.options.pageNum;
  24. var buttonNum = this.options.buttonNum;
  25. var content = [];
  26. content.push("<ul>");
  27. content.push("<li class='xl-prevPage'>上一页</li>");
  28. //页面总数小于等于当前要展示页数总数,展示所有页面
  29. if(pageNum <= buttonNum){
  30. for(var i=1; i<=pageNum; i++){
  31. if (nowPage !== i) {
  32. content.push("<li>"+i+"</li>");
  33. } else {
  34. content.push("<li class='xl-active'>"+i+"</li>");
  35. }
  36. }
  37. }else if(nowPage <= Math.floor(buttonNum / 2)){
  38. //当前页面小于等于展示页数总数的一半(向下取整),从1开始
  39. for(var i=1;i<= buttonNum-2;i++){
  40. if (nowPage !== i) {
  41. content.push("<li>"+i+"</li>");
  42. } else {
  43. content.push("<li class='xl-active'>"+i+"</li>");
  44. }
  45. }
  46. content.push("<li class='xl-disabled'>...</li>");
  47. content.push("<li>" + pageNum + "</li>");
  48. }else if(pageNum - nowPage <= Math.floor(buttonNum / 2)){
  49. //当前页面大于展示页数总数的一半(向下取整)
  50. content.push("<li>"+1+"</li>");
  51. content.push("<li class='xl-disabled'>...</li>");
  52. for(var i=pageNum-buttonNum+3; i<=pageNum; i++){
  53. if (nowPage !== i) {
  54. content.push("<li>"+i+"</li>");
  55. } else {
  56. content.push("<li class='xl-active'>"+i+"</li>");
  57. }
  58. }
  59. }else{
  60. //前半部分页码
  61. if(nowPage - Math.floor(buttonNum / 2) <= 0){
  62. for(var i=1;i<= Math.floor(buttonNum / 2);i++){
  63. if (nowPage !== i) {
  64. content.push("<li>"+i+"</li>");
  65. } else {
  66. content.push("<li class='xl-active'>"+i+"</li>");
  67. }
  68. }
  69. }else{
  70. content.push("<li>"+1+"</li>");
  71. content.push("<li class='xl-disabled'>...</li>");
  72. for(var i=nowPage-Math.floor(buttonNum / 2)+(buttonNum % 2 == 0 ? 3: 2); i<=nowPage; i++){
  73. if (nowPage !== i) {
  74. content.push("<li>"+i+"</li>");
  75. } else {
  76. content.push("<li class='xl-active'>"+i+"</li>");
  77. }
  78. }
  79. }
  80. //后半部分页码
  81. if(pageNum - nowPage <= 0){
  82. for(var i=nowPage+1;i<=pageNum;i++){
  83. content.push("<li>" + i + "</li>");
  84. }
  85. }else{
  86. for(var i=nowPage+1; i<=nowPage+Math.floor(buttonNum / 2)-2; i++){
  87. content.push("<li>"+i+"</li>");
  88. }
  89. content.push("<li class='xl-disabled'>...</li>");
  90. content.push("<li>" + pageNum + "</li>");
  91. }
  92. }
  93. content.push("<li class='xl-nextPage'>下一页</li>");
  94. content.push("</ul>");
  95. me.element.html(content.join(''));
  96. // DOM重新生成后每次调用是否禁用button
  97. setTimeout(function () {
  98. me.disabled();
  99. }, 20);
  100. },
  101. bindClickEvent: function(){
  102. var me = this;
  103. me.element.off('click', 'li');
  104. me.element.on('click', 'li', function () {
  105. var cla = $(this).attr('class');
  106. var num = parseInt($(this).html());
  107. var nowPage = me.options.nowPage;
  108. if( $(this).hasClass('xl-disabled')){
  109. return ;
  110. }
  111. if (cla === 'xl-prevPage') {
  112. if (nowPage !== 1) {
  113. me.options.nowPage -= 1;
  114. }
  115. } else if (cla === 'xl-nextPage') {
  116. if (nowPage !== me.options.pageNum) {
  117. me.options.nowPage += 1;
  118. }
  119. }else {
  120. me.options.nowPage = num;
  121. }
  122. me.createHtml();
  123. if (me.options.callback) {
  124. me.options.callback(me.options.nowPage);
  125. }
  126. });
  127. },
  128. disabled: function () {
  129. var me = this;
  130. var nowPage = me.options.nowPage;
  131. var pageNum = me.options.pageNum;
  132. if (nowPage === 1) {
  133. me.element.children().children('.xl-prevPage').addClass('xl-disabled');
  134. } else if (nowPage === pageNum) {
  135. me.element.children().children('.xl-nextPage').addClass('xl-disabled');
  136. }
  137. }
  138. }
  139. $.fn.paging = function (options) {
  140. return new Paging($(this), options);
  141. }
  142. })(jQuery, window, document);