You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

133 lines
4.3 KiB

  1. //分页插件
  2. /**
  3. * 2015-11-09
  4. * @author by futural
  5. */
  6. (function($){
  7. var ms = {
  8. init:function(obj,args){
  9. return (function(){
  10. ms.fillHtml(obj,args);
  11. ms.bindEvent(obj,args);
  12. })();
  13. },
  14. //填充html
  15. fillHtml:function(obj,args){
  16. return (function(){
  17. obj.empty();
  18. //上一页
  19. if(args.current > 1){
  20. obj.append('<a href="javascript:;" class="prevPage"><上一页</a>');
  21. }else{
  22. obj.remove('.prevPage');
  23. obj.append('<span class="disabled"><上一页</span>');
  24. }
  25. //中间页码
  26. if(args.current != 1 && args.current >= 4 && args.pageCount != 4){
  27. obj.append('<a href="javascript:;" class="tcdNumber">'+1+'</a>');
  28. }
  29. if(args.current-2 > 2 && args.current <= args.pageCount && args.pageCount > 5){
  30. obj.append('<span>...</span>');
  31. }
  32. var start = args.current -2,end = args.current+2;
  33. if((start > 1 && args.current < 4)||args.current == 1){
  34. end++;
  35. }
  36. if(args.current > args.pageCount-4 && args.current >= args.pageCount){
  37. start--;
  38. }
  39. for (;start <= end; start++) {
  40. if(start <= args.pageCount && start >= 1){
  41. if(start != args.current){
  42. obj.append('<a href="javascript:;" class="tcdNumber">'+ start +'</a>');
  43. }else{
  44. obj.append('<span class="current">'+ start +'</span>');
  45. }
  46. }
  47. }
  48. if(args.current + 2 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 5){
  49. obj.append('<span>...</span>');
  50. }
  51. if(args.current != args.pageCount && args.current < args.pageCount -2 && args.pageCount != 4){
  52. obj.append('<a href="javascript:;" class="tcdNumber">'+args.pageCount+'</a>');
  53. }
  54. //下一页
  55. if(args.current < args.pageCount){
  56. obj.append('<a href="javascript:;" class="nextPage">下一页></a>');
  57. }else{
  58. obj.remove('.nextPage');
  59. obj.append('<span class="disabled">下一页></span>');
  60. }
  61. obj.append('<span class="pagecount">共'+args.pageCount+'页</span>');
  62. //跳转页码
  63. if(args.turndown == 'true'){
  64. obj.append('<span class="countYe">到第<input type="text" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')" maxlength='+args.pageCount.toString().length+'>页<a href="javascript:;" class="turndown">确定</a><span>');
  65. }
  66. })();
  67. },
  68. //绑定事件
  69. bindEvent:function(obj,args){
  70. return (function(){
  71. obj.on("click","a.tcdNumber",function(){
  72. var current = parseInt($(this).text());
  73. ms.fillHtml(obj,{"current":current,"pageCount":args.pageCount,"turndown":args.turndown});
  74. if(typeof(args.backFn)=="function"){
  75. args.backFn(current);
  76. }
  77. });
  78. //上一页
  79. obj.on("click","a.prevPage",function(){
  80. var current = parseInt(obj.children("span.current").text());
  81. ms.fillHtml(obj,{"current":current-1,"pageCount":args.pageCount,"turndown":args.turndown});
  82. if(typeof(args.backFn)=="function"){
  83. args.backFn(current-1);
  84. }
  85. });
  86. //下一页
  87. obj.on("click","a.nextPage",function(){
  88. var current = parseInt(obj.children("span.current").text());
  89. ms.fillHtml(obj,{"current":current+1,"pageCount":args.pageCount,"turndown":args.turndown});
  90. if(typeof(args.backFn)=="function"){
  91. args.backFn(current+1);
  92. }
  93. });
  94. //跳转
  95. obj.on("click","a.turndown",function(){
  96. var page = $("span.countYe input").val();
  97. if(page.trim()==""){page=1;}
  98. page=parseInt(page);
  99. if(page==0){page=1;}
  100. if (page > args.pageCount) {
  101. alert("您的输入有误,请重新输入!");
  102. $("span.countYe input").val('');
  103. $("span.countYe input").focus();
  104. return;
  105. }
  106. ms.fillHtml(obj,{"current":page,"pageCount":args.pageCount,"turndown":args.turndown});
  107. if(typeof(args.backFn)=="function"){
  108. args.backFn(page);
  109. }
  110. // var page = parseInt($("span.countYe input").val());
  111. // if (page > args.pageCount) {
  112. // alert("您的输入有误,请重新输入!");
  113. // return;
  114. // }
  115. // ms.fillHtml(obj,{"current":page,"pageCount":args.pageCount,"turndown":args.turndown});
  116. // if(typeof(args.backFn)=="function"){
  117. // args.backFn(page);
  118. // }
  119. });
  120. })();
  121. }
  122. }
  123. $.fn.createPage = function(options){
  124. var args = $.extend({
  125. pageCount : 10,
  126. current : 1,
  127. turndown:true,
  128. backFn : function(){}
  129. },options);
  130. ms.init(this,args);
  131. }
  132. })(jQuery);