1+ /**
2+ * jquery.pager.js v0.1.0
3+ * MIT License
4+ * author info pls visit: http://luopq.com
5+ * for more info pls visit: https://github.com/LuoPQ/jquery.pager.js
6+ */
7+ ; ( function ( $ , window , document , undefined ) {
8+ "use strict" ;
9+ var defaults = {
10+ pageIndex : 0 ,
11+ pageSize : 6 ,
12+ itemCount : 50 ,
13+ maxButtonCount : 7 ,
14+ prevText : "上一页" ,
15+ nextText : "下一页" ,
16+ buildPageUrl : null ,
17+ onPageChanged : null
18+ } ;
19+
20+ function Pager ( $ele , options ) {
21+ this . $ele = $ele ;
22+ this . options = options = $ . extend ( defaults , options || { } ) ;
23+ this . init ( ) ;
24+ }
25+ Pager . prototype = {
26+ constructor : Pager ,
27+ init : function ( ) {
28+ this . renderHtml ( ) ;
29+ this . bindEvent ( ) ;
30+ } ,
31+ renderHtml : function ( ) {
32+ var options = this . options ;
33+
34+ options . pageCount = Math . ceil ( options . itemCount / options . pageSize ) ;
35+ var html = [ ] ;
36+
37+ //生成上一页的按钮
38+ if ( options . pageIndex > 0 ) {
39+ html . push ( '<a page="' + ( options . pageIndex - 1 ) + '" href="' + this . buildPageUrl ( options . pageIndex + 1 ) + '" class="flip">' + options . prevText + '</a>' ) ;
40+ } else {
41+ html . push ( '<span class="flip noPage">' + options . prevText + '</span>' ) ;
42+ }
43+
44+ //这里是关键
45+ //临时的起始页码中间页码,当页码数量大于显示的最大按钮数时使用
46+ var tempStartIndex = options . pageIndex - Math . floor ( options . maxButtonCount / 2 ) + 1 ;
47+
48+ //计算终止页码,通过max计算一排按钮中的第一个按钮的页码,然后计算出页数量
49+ var endIndex = Math . min ( options . pageCount , Math . max ( 0 , tempStartIndex ) + options . maxButtonCount ) - 1 ;
50+ var startIndex = Math . max ( 0 , endIndex - options . maxButtonCount + 1 ) ;
51+
52+ // 第一页
53+ if ( startIndex > 0 ) {
54+ html . push ( "<a href='" + this . buildPageUrl ( 0 ) + "' page='" + 0 + "'>1</a> " ) ;
55+ html . push ( "<span>...</span>" ) ;
56+ }
57+
58+ //生成页码按钮
59+ for ( var i = startIndex ; i <= endIndex ; i ++ ) {
60+ if ( options . pageIndex == i ) {
61+ html . push ( '<span class="curPage">' + ( i + 1 ) + '</span>' ) ;
62+ } else {
63+ html . push ( '<a page="' + i + '" href="' + this . buildPageUrl ( options . pageIndex + 1 ) + '">' + ( i + 1 ) + '</a>' ) ;
64+ }
65+ }
66+
67+ // 最后一页
68+ if ( endIndex < options . pageCount - 1 ) {
69+ html . push ( "<span>...</span> " ) ;
70+ html . push ( "<a href='" + this . buildPageUrl ( options . pageCount - 1 ) + "' page='" + ( options . pageCount - 1 ) + "'>" + options . pageCount + "</a> " ) ;
71+ }
72+
73+ //生成下一页的按钮
74+ if ( options . pageIndex < options . pageCount - 1 ) {
75+ html . push ( '<a page="' + ( options . pageIndex + 1 ) + '" href="' + this . buildPageUrl ( options . pageIndex + 1 ) + '" class="flip">' + options . nextText + '</a>' ) ;
76+ } else {
77+ html . push ( '<span class="flip noPage">' + options . nextText + '</span>' ) ;
78+ }
79+
80+ this . $ele . html ( html . join ( "" ) ) ;
81+ } ,
82+ bindEvent : function ( ) {
83+ var that = this ;
84+ that . $ele . on ( "click" , "a" , function ( ) {
85+ that . options . pageIndex = parseInt ( $ ( this ) . attr ( "page" ) , 10 ) ;
86+ that . renderHtml ( ) ;
87+ that . options . onPageChanged && that . options . onPageChange ( that . options . pageIndex ) ;
88+ } )
89+ } ,
90+ buildPageUrl : function ( ) {
91+ if ( $ . isFunction ( this . options . buildPageUrl ) ) {
92+ return this . options . buildPageUrl ( pageIndex ) ;
93+ }
94+ return "javascript:;" ;
95+ } ,
96+ getPageIndex : function ( ) {
97+ return this . options . pageIndex ;
98+ } ,
99+ setPageIndex : function ( pageIndex ) {
100+ this . options . pageIndex = pageIndex ;
101+ this . renderHtml ( ) ;
102+ } ,
103+ setItemCount : function ( itemCount ) {
104+ this . options . pageIndex = 0 ;
105+ this . options . itemCount = itemCount ;
106+ this . renderHtml ( ) ;
107+ }
108+ } ;
109+
110+
111+ $ . fn . pager = function ( options ) {
112+ options = $ . extend ( defaults , options || { } ) ;
113+
114+ return new Pager ( $ ( this ) , options ) ;
115+ }
116+
117+ } ) ( jQuery , window , document ) ;
0 commit comments