提交 | 用户 | 时间
|
58d006
|
1 |
/* |
A |
2 |
* author:mengly |
|
3 |
* date:2014-2-2, |
|
4 |
* todo:目前的格式比较接近于bootstrap,需要以后修改 |
|
5 |
* |
|
6 |
*/ |
|
7 |
(function($,window){ |
|
8 |
$.fn.bootpag = function(options){ |
|
9 |
|
|
10 |
var $owner = this, |
|
11 |
settings = $.extend({ |
|
12 |
total: 0, |
|
13 |
page: 1, |
|
14 |
maxVisible: 10, |
|
15 |
leaps: true, |
|
16 |
href: 'javascript:void(0);', |
|
17 |
hrefVariable: '{{number}}', |
|
18 |
next: '>', |
|
19 |
prev: '<', |
|
20 |
callback:function(num){return false;} |
|
21 |
}, |
|
22 |
$owner.settings || {}, |
|
23 |
options || {}); |
|
24 |
|
|
25 |
if(settings.total <= 0) |
|
26 |
return this; |
|
27 |
|
|
28 |
if(!settings.maxVisible){ |
|
29 |
settings.maxVisible = settings.total; |
|
30 |
} |
|
31 |
|
|
32 |
$owner.settings = settings; |
|
33 |
|
|
34 |
function renderPage($bootpag, page){ |
|
35 |
$bootpag.empty(); |
|
36 |
$owner.trigger('page', page); |
|
37 |
create($bootpag,page); |
|
38 |
} |
|
39 |
|
|
40 |
|
|
41 |
function href(c){ |
|
42 |
|
|
43 |
return settings.href.replace(settings.hrefVariable, c); |
|
44 |
} |
|
45 |
function getstart(total,max,page){ |
|
46 |
if(page>total) |
|
47 |
return -1; |
|
48 |
if(total<2 || max<=2||total<=max) |
|
49 |
return 2; |
|
50 |
var tmv=max-2; |
|
51 |
var htmv=parseInt(tmv/2); |
|
52 |
if((page-1)<htmv) |
|
53 |
return 2; |
|
54 |
if((total-page)<htmv) |
|
55 |
return (total-tmv)>2?(total-tmv):2; |
|
56 |
return page-htmv; |
|
57 |
|
|
58 |
} |
|
59 |
return this.each(function(){ |
|
60 |
var $bootpag, lp, me = $(this); |
|
61 |
p=[]; |
|
62 |
if(settings.total<=1||settings.maxVisible<=1) |
|
63 |
return; |
|
64 |
var start=getstart(settings.total,settings.maxVisible,settings.page); |
|
65 |
// if(start<=1) |
|
66 |
// return; |
|
67 |
if(start<0) |
|
68 |
return; |
|
69 |
if(start<2) |
|
70 |
start=2; |
|
71 |
var total=settings.total; |
|
72 |
var maxVisible=settings.maxVisible; |
|
73 |
var page=settings.page; |
|
74 |
p.push('<a class="prev" rev="'+((page-1)>0?1:(page-1))+'" href="'+href(((page-1)>0?(page-2):0))+'">'+settings.prev+'</a>'); |
|
75 |
p.push('<a class="1" rev="1" href="'+href(0)+'">'+1+'</a>'); |
|
76 |
if(start-1>1) |
|
77 |
p.push('...'); |
|
78 |
|
|
79 |
var i = start |
|
80 |
for (; i <=Math.min(start+settings.maxVisible-3,settings.total-1); i++) { |
|
81 |
p.push('<a class="1" rev="'+i+'" href="'+href(i-1)+'">'+i+'</a>'); |
|
82 |
}; |
|
83 |
if(total-i>0) |
|
84 |
p.push('...'); |
|
85 |
p.push('<a class="1" rev="'+settings.total+'" href="'+href(settings.total-1)+'">'+settings.total+'</a>'); |
|
86 |
p.push('<a class="next" rev="'+((page+1)>total?total:(page+1))+'" href="'+href(((page+1)>total?(page):total-1))+'">'+settings.next+'</a>'); |
|
87 |
jQuery(p.join('')).appendTo(me); |
|
88 |
me.find('a[rev="'+page+'"]').addClass('current'); |
|
89 |
me.on('click','a',function(){ |
|
90 |
var me1=$(this); |
|
91 |
var callback=settings.callback; |
|
92 |
callback(me1.attr('rev')); |
|
93 |
}); |
|
94 |
}); |
|
95 |
} |
|
96 |
})(jQuery,window); |
|
97 |
|