当前位置:Gxlcms > JavaScript > 最实用的jQuery分页插件

最实用的jQuery分页插件

时间:2021-07-01 10:21:17 帮助过:10人阅读

在做商城和订单管理的时候,常常会用到分页功能,所以我封装了一个jQuery的分页插件,该插件主要实现上下翻页,输入数字跳转等功能。

具体实现如下:

输入参数需要当前页码pageNo,总页码totalPage,回调函数callback。

主要的实现有两个函数,一个是根据当前页和总页数生成相应的html的代码,一个是事件绑定及回调函数的执行。

creatHtml函数:

  1. creatHtml:function(){
  2. var me=this;
  3. var content="";
  4.   //当前页码
  5. var current=me.options.pageNo;
  6.   //总页码
  7. var total=me.options.totalPage;
  8.   //当前页码大于1显示向上翻页按钮
  9. if(current > 1){
  10. content += "<a><</a>";
  11. }
  12.   //总页数大于7,根据当前页显示省略号,否则显示全部页码
  13. if(total > 7){
  14. if(current < 4){
  15. for(var i=1;i<7;i++){
  16. if(current==i){
  17. content += "<a class='current'>"+i+"</a>";
  18. }else{
  19. content += "<a>"+i+"</a>";
  20. }
  21. }
  22. content += "...";
  23. content += "<a>"+total+"</a>";
  24. }else{
  25. if(current < total - 3){
  26. content += "<a name='1' type='button' class='page num'>1</a>";
  27. content += "...";
  28. for(var i=current-2;i<current+3;i++){
  29. if(current==i){
  30. content += "<a class='current'>"+i+"</a>";
  31. }else{
  32. content += "<a>"+i+"</a>";
  33. }
  34. }
  35. content += "...";
  36. content += "<a>"+total+"</a>";
  37. }else{
  38. content += "<a>1</a>";
  39. content += "...";
  40. for(var i=total-5;i<total+1;i++){
  41. if(current==i){
  42. content += "<a class='current'>"+i+"</a>";
  43. }else{
  44. content += "<a>"+i+"</a>";
  45. }
  46. }
  47. }
  48. }
  49. }else{
  50. for(var i=1;i<total+1;i++){
  51. if(current==i){
  52. content += "<a class='current'>"+i+"</a>";
  53. }else{
  54. content += "<a>"+i+"</a>";
  55. }
  56. }
  57. }
  58.   //当前页小于总页数,显示向下翻页按钮
  59. if(current < total){
  60. content += "<a>></a>";
  61. }
  62.   //输入跳转
  63. content += " 到第 ";
  64. content += "<input max='3' maxlength='3' value='"+current+"' type='text' />";
  65. content += " 页 ";
  66. content += "<a>Go</a>";
  67.   //更新HTML
  68. me.element.html(content);
  69. }

bindEvent函数:

  1. bindEvent:function(){
  2. var me=this;
  3.   //分页点击事件
  4. me.element.on('click','a',function(){
  5. var num=$(this).html();
  6. if(num=="<"){//上翻
  7. me.options.pageNo=+me.options.pageNo-1;
  8. }else if(num==">"){//下翻
  9. me.options.pageNo=+me.options.pageNo+1;
  10. }else if(num=="Go"){//输入页码跳转
  11. var ipt=+me.element.find('input').val();
  12. if(ipt&&ipt<=me.options.totalPage&&ipt!=me.options.pageNo){
  13. me.options.pageNo=ipt;
  14. }
  15. }else{//直接跳转
  16. me.options.pageNo=+num;
  17. }
  18.     //更新html
  19. me.creatHtml();
  20.     //调用回调函数,返回当前页码
  21. if(me.options.callback){
  22. me.options.callback(me.options.pageNo);
  23. }
  24. });
  25. }

将函数封装起来,完整如下:

  1. ;(function($,window,document,undefined){
  2. var initDate={
  3. pageNo:1,
  4. totalPage:1,
  5. callback:function(){}
  6. };
  7. function Paging(element,options){
  8. this.element = element;
  9. this.options=options=$.extend(initDate,options||{});
  10. this.init();
  11. }
  12. Paging.prototype={
  13. constructor:Paging,
  14. init:function(){
  15. this.creatHtml();
  16. this.bindEvent();
  17. },
  18. creatHtml:function(){
  19. var me=this;
  20. var content="";
  21. var current=me.options.pageNo;
  22. var total=me.options.totalPage;
  23. if(current > 1){
  24. content += "<a><</a>";
  25. }
  26. if(total > 7){
  27. if(current < 4){
  28. for(var i=1;i<7;i++){
  29. if(current==i){
  30. content += "<a class='current'>"+i+"</a>";
  31. }else{
  32. content += "<a>"+i+"</a>";
  33. }
  34. }
  35. content += "...";
  36. content += "<a>"+total+"</a>";
  37. }else{
  38. if(current < total - 3){
  39. content += "<a name='1' type='button' class='page num'>1</a>";
  40. content += "...";
  41. for(var i=current-2;i<current+3;i++){
  42. if(current==i){
  43. content += "<a class='current'>"+i+"</a>";
  44. }else{
  45. content += "<a>"+i+"</a>";
  46. }
  47. }
  48. content += "...";
  49. content += "<a>"+total+"</a>";
  50. }else{
  51. content += "<a>1</a>";
  52. content += "...";
  53. for(var i=total-5;i<total+1;i++){
  54. if(current==i){
  55. content += "<a class='current'>"+i+"</a>";
  56. }else{
  57. content += "<a>"+i+"</a>";
  58. }
  59. }
  60. }
  61. }
  62. }else{
  63. for(var i=1;i<total+1;i++){
  64. if(current==i){
  65. content += "<a class='current'>"+i+"</a>";
  66. }else{
  67. content += "<a>"+i+"</a>";
  68. }
  69. }
  70. }
  71. if(current < total){
  72. content += "<a>></a>";
  73. }
  74. content += " 到第 ";
  75. content += "<input max='3' maxlength='3' value='"+current+"' type='text' />";
  76. content += " 页 ";
  77. content += "<a>Go</a>";
  78. me.element.html(content);
  79. },
  80. bindEvent:function(){
  81. var me=this;
  82. me.element.on('click','a',function(){
  83. var num=$(this).html();
  84. if(num=="<"){
  85. me.options.pageNo=+me.options.pageNo-1;
  86. }else if(num==">"){
  87. me.options.pageNo=+me.options.pageNo+1;
  88. }else if(num=="Go"){
  89. var ipt=+me.element.find('input').val();
  90. if(ipt&&ipt<=me.options.totalPage&&ipt!=me.options.pageNo){
  91. me.options.pageNo=ipt;
  92. }
  93. }else{
  94. me.options.pageNo=+num;
  95. }
  96. me.creatHtml();
  97. if(me.options.callback){
  98. me.options.callback(me.options.pageNo);
  99. }
  100. });
  101. }
  102. };
  103. $.fn.paging=function(options){
  104. options=$.extend(initDate,options||{});
  105. return new Paging($(this),options);
  106. }
  107. })(jQuery,window,document);

HTML:

<div id="page"></div>

js引用:

  1. $('#page').paging({pageNo:2,totalPage:10,callback:function(cur){
  2. console.log(‘当前页:'+cur);//当前页:2
  3. }});

这里加了一些简单的样式,可以根据具体的ui设计来设计样式。

  1. <style type="text/css">
  2. a{
  3. width: 23px;
  4. height: 23px;
  5. border: 1px solid #dce0e0;
  6. text-align: center;
  7. margin: 0 4px;
  8. cursor: pointer;
  9. display: inline-block;
  10. }
  11. .current{
  12. background-color: #5ac3e7;
  13. }
  14. </style>

github地址:https://github.com/Martian1/jQuery.paging.js

更多精彩内容请点击:jquery分页功能汇总进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行