paging.js(基于jQuery)

(function($){
    //default properties.
    var a=/a/i,defs={
        item:'a',next:'>{5}',prev:'{4}<',format:'{0}',
        itemClass:'btn btn-green',sideClass:'btn btn-green',
        itemCurrent:'active',length:5,max:1,current:1,append:false
        ,href:'#{0}',event:true,first:'1<<',last:'>>{6}'
    },format=function(str){
        var arg=arguments;
        return str.replace(/\{(\d+)\}/g,function(m,d) {
            if(+d<0) return m;
            else return arg[+d+1]||"";
        });
    },item,make=function(op,page,cls,str){
        item=document.createElement(op.item);
        item.className=cls;
        item.innerHTML=format(str,page,op.length,op.start,op.end,op.start-1,op.end+1,op.max);
        if(a.test(op.item)) item.href=format(op.href,page);
        if(op.event){
            $(item).bind('click',function(e){
                var fired=true;
                if($.isFunction(op.onclick)) fired=op.onclick.call(item,e,page,op);
                if(fired==undefined||fired)
                    op.origin.paging($.extend({},op,{current:page}));
                return fired;
            }).appendTo(op.origin);
            //bind event for each elements.
            var ev='on';
            switch(str){
                case op.prev:ev+='prev';break;
                case op.next:ev+='next';break;
                case op.first:ev+='first';break;
                case op.last:ev+='last';break;
                default:ev+='item';break;
            }
            if($.isFunction(op[ev])) op[ev].call(item,page,op);
        }
        return item;
    };

    $.fn.paging=function(op){
        op=$.extend({origin:this},defs,op||{});this.html('');
        if(op.max<1) op.max=1; if(op.current<1) op.current=1;
        op.start=Math.floor((op.current-1)/op.length)*op.length+1;
        op.end=op.start-1+op.length;
        if(op.end>op.max) op.end=op.max;
        if(!op.append) this.empty();
        //prev button
        if(op.current>op.length){
            if(op.first!==false) make(op,1,op.sideClass,op.first);
            make(op,op.start-1,op.sideClass,op.prev);
        }
        //pages button
        for(var i=op.start;i<=op.end;i++)
            make(op,i,op.itemClass+(i==op.current?' '+op.itemCurrent:''),op.format);
        //next button
        if(op.current<=Math.floor(op.max/op.length)*op.length){
            make(op,op.end+1,op.sideClass,op.next);
            if(op.last!==false) make(op,op.max,op.sideClass,op.last);
        }

        //last button
    };
})(jQuery);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值