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