首页 > 语言 > JavaScript > 正文

使用JQuery实现的分页插件分享

2024-05-06 16:24:56
字体:
来源:转载
供稿:网友

本文给大家总结了几种使用jQuery实现的分页插件,效果非常不错,有需要的小伙伴可以参考下。

一个简单的jQuery分页插件,兼容AMD规范和requireJS.

 

 
  1. /** 
  2. * jQuery分页插件 
  3. * */ 
  4. ;(function (factory) { 
  5. if (typeof define === "function" && define.amd) { 
  6. // AMD模式 
  7. define([ "jquery" ], factory); 
  8. else { 
  9. // 全局模式 
  10. factory(jQuery); 
  11. }(function ($) { 
  12.  
  13. //定义MyPagePlugin的构造函数 
  14. MyPagePlugin = function(ele, option) { 
  15. // this.viewHtml="<nav><ul class='pagination'><li><a id='firstPageli'>«</a></li><li><a id='prevPageli'>‹</a></li><li class='active'><a>第<span id='curPageNoSpan'></span>页,共<span id='allPageCountSpan'></span>页</a></li><li><a id='nextPageli'>›</a></li><li><a id='lastPageli'>»</a></li></ul></nav>"; 
  16. this.viewHtml= "<div class='pageplugin'><a class='first firstPageli'>«</a><a class='previous prevPageli'>‹</a><a class='present'>第<span class='curPageNoSpan'></span>页,共<span class='allPageCountSpan'></span>页</a><a class='next nextPageli'>›</a><a class='last lastPageli'>»</a></div>" 
  17.  
  18. this.$element = ele; 
  19. /**参数:page:当前页,pageCount:总共页数,onPaged回调函数,回调函数会传入页数*/ 
  20. this.defaults = { 
  21. page:1, 
  22. pageCount:1, 
  23. onPaged:function(pageNo){} 
  24. }; 
  25. this.options = $.extend({}, this.defaults, option); 
  26.  
  27. //定义MyPagePlugin的方法 
  28. MyPagePlugin.prototype = { 
  29. initPlugin:function(){ 
  30. this.$element.empty(); 
  31. this.$element.append(this.viewHtml); 
  32. this.options.onPaged(this.options.page);//初始化 
  33. this.$element.find(".curPageNoSpan").text(this.options.page); 
  34. this.$element.find(".curPageNoSpan").data("options",this.options); 
  35. this.$element.find(".allPageCountSpan").text(this.options.pageCount); 
  36. this.$element.find(".firstPageli").on("click",function(e){ 
  37.  
  38. var curNo=$(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(); 
  39. curNo=parseInt(curNo); 
  40. if(curNo==1){ 
  41. return false
  42. }else
  43.  
  44. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").data("options").onPaged(1); 
  45. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(1); 
  46. return false
  47. }); 
  48. this.$element.find(".prevPageli").on("click",function(e){ 
  49. var curNo=$(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(); 
  50. curNo=parseInt(curNo); 
  51. if(curNo==1){ 
  52. return false
  53. }else
  54. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").data("options").onPaged(curNo-1); 
  55. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(curNo-1); 
  56. return false
  57. }); 
  58. this.$element.find(".nextPageli").on("click",function(e){ 
  59. var curNo=$(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(); 
  60. curNo=parseInt(curNo); 
  61. var pageCount=$(e.currentTarget).parent("div.pageplugin").find(".allPageCountSpan").text(); 
  62. pageCount=parseInt(pageCount); 
  63. if(curNo==pageCount){ 
  64. return false
  65. }else
  66. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").data("options").onPaged(curNo+1); 
  67. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(curNo+1); 
  68. return false
  69. }); 
  70. this.$element.find(".lastPageli").on("click",function(e){ 
  71. var curNo=$(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(); 
  72. curNo=parseInt(curNo); 
  73. var pageCount=$(e.currentTarget).parent("div.pageplugin").find(".allPageCountSpan").text(); 
  74. pageCount=parseInt(pageCount); 
  75. if(curNo==pageCount){ 
  76. return false
  77. }else
  78. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").data("options").onPaged(pageCount); 
  79. $(e.currentTarget).parent("div.pageplugin").find(".curPageNoSpan").text(pageCount); 
  80. return false
  81. }); 
  82.  
  83.  
  84.  
  85. $.fn.pagePlugin = function (option) { 
  86. var pagePlugin=new MyPagePlugin(this,option); 
  87. pagePlugin.initPlugin(); 
  88. }; 
  89. })); 

CSS

 

 
  1. .pageplugin { 
  2. display: inline-block; 
  3. border: 1px solid #CDCDCD; 
  4. border-radius: 3px; } 
  5.  
  6. .pageplugin a { 
  7. cursor: pointer; 
  8. display: block; 
  9. float: left; 
  10. width: 20px; 
  11. height: 20px; 
  12. outline: none; 
  13. border-right: 1px solid #CDCDCD; 
  14. border-left: 1px solid #CDCDCD; 
  15. color: #767676; 
  16. vertical-align: middle; 
  17. text-align: center; 
  18. text-decoration: none; 
  19. font-weight: bold; 
  20. font-size: 16px; 
  21. font-family: Times, 'Times New Roman', Georgia, Palatino; 
  22. background-color: #f7f7f7; 
  23. /* ATTN: need a better font stack  
  24. background-color: #f7f7f7; 
  25. background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #f3f3f3), color-stop(100%, lightgrey)); 
  26. background-image: -webkit-linear-gradient(#f3f3f3, lightgrey); 
  27. background-image: linear-gradient(#f3f3f3, lightgrey); */
  28. .pageplugin a:hover, .pageplugin a:focus, .pageplugin a:active { 
  29. color:#0099CC; 
  30. background-color: #cecece; 
  31. background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #e4e4e4), color-stop(100%, #cecece)); 
  32. background-image: -webkit-linear-gradient(#e4e4e4, #cecece); 
  33. background-image: linear-gradient(#e4e4e4, #cecece); } 
  34. .pageplugin a.disabled, .pageplugin a.disabled:hover, .pageplugin a.disabled:focus, .pageplugin a.disabled:active { 
  35. background-color: #f3f3f3; 
  36. background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #f3f3f3), color-stop(100%, lightgrey)); 
  37. background-image: -webkit-linear-gradient(#f3f3f3, lightgrey); 
  38. background-image: linear-gradient(#f3f3f3, lightgrey); 
  39. color: #A8A8A8; 
  40. cursor: default; } 
  41.  
  42. .pageplugin a:first-child { 
  43. border: none; 
  44. border-radius: 2px 0 0 2px; } 
  45.  
  46. .pageplugin a:last-child { 
  47. border: none; 
  48. border-radius: 0 2px 2px 0; } 
  49.  
  50. .pageplugin .present { 
  51. float: left; 
  52. margin: 0; 
  53. padding: 0; 
  54. width: 120px; 
  55. height: 20px; 
  56. outline: none; 
  57. border: none; 
  58. vertical-align: middle; 
  59. text-align: center; } 

jquery分页插件cypager

cypager是网友分享到JquerySchool网站上的一款作品,非常实用,经过测试,插件兼容 IE8+,Chrome,Firefox 浏览器,核心文件仅 5KB。。。

调用方式

由于是 jquery插件,所以在引人 cypager.min.js 之前,要引人 jquery.min.js 本人使用的是 1.7.2 版本的,低版本的没试过。

引入css :

引人js :

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选