jQuery pager.js 插件动态分页功能实例分析

function Page(opt){ var set = $.extend({num:null,startnum:1,elem:null,callback:null},opt||{}); if(set.startnum>set.num||set.startnum<1){ set.startnum = 1; } var n = 0,htm = ''; var clickpages = { elem:set.elem, num:set.num, callback:set.callback, init:function(){ this.elem.next('div.pageJump').children('.button').unbind('click') this.JumpPages(); this.elem.children('li').click(function () { var txt = $(this).children('a').text(); var page = '', ele = null; var page1 = parseInt(clickpages.elem.children('li.active').attr('page')); if (isNaN(parseInt(txt))) { switch (txt) { case '下一页': if (page1 == clickpages.num) { return; } if (page1 >= (clickpages.num - 2) || clickpages.num <= 6 || page1 < 3) { ele = clickpages.elem.children('li.active').next(); } else { clickpages.newPages('next', page1 + 1); ele = clickpages.elem.children('li.active'); } break; case '上一页': if (page1 == '1') { return; } if (page1 >= (clickpages.num - 1) || page1 <= 3 || clickpages.num <= 6) { ele = clickpages.elem.children('li.active').prev(); } else { clickpages.newPages('prev', page1 - 1); ele = clickpages.elem.children('li.active'); } break; case '首页': if (page1 == '1') { return; } if (clickpages.num > 6) { clickpages.newPages('首页', 1); } ele = clickpages.elem.children('li[page=1]'); break; case '尾页': if (page1 == clickpages.num) { return; } if (clickpages.num > 6) { clickpages.newPages('尾页', clickpages.num); } ele = clickpages.elem.children('li[page=' + clickpages.num + ']'); break; case '...': return; } } else { // if ((parseInt(txt) >= (clickpages.num - 3) || parseInt(txt) <= 3) && clickpages.num > 6) { // clickpages.newPages('jump', parseInt(txt)); // } // ele = $(this); // } // page = clickpages.actPages(ele); // if (page != '' && page != page1) { // if (clickpages.callback){ // clickpages.callback(parseInt(page)); // } var i = parseInt(txt); if(isNaN(i)||(i<=0)||i>clickpages.num){ return; }else if(clickpages.num>6){ clickpages.newPages('jump',i); }else{ var ele = clickpages.elem.children('li[page='+i+']'); clickpages.actPages(ele); if (clickpages.callback){ clickpages.callback(i); } return; } if (clickpages.callback){ clickpages.callback(i); } } }); }, //active actPages:function (ele) { ele.addClass('active').siblings().removeClass('active'); return clickpages.elem.children('li.active').text(); }, JumpPages:function () { this.elem.next('div.pageJump').children('.button').click(function(){ var i = parseInt($(this).siblings('input').val()); if(isNaN(i)||(i<=0)||i>clickpages.num){ return; }else if(clickpages.num>6){ clickpages.newPages('jump',i); }else{ var ele = clickpages.elem.children('li[page='+i+']'); clickpages.actPages(ele); if (clickpages.callback){ clickpages.callback(i); } return; } if (clickpages.callback){ clickpages.callback(i); } }) }, //newpages newPages:function (type, i) { var html = "",htmlLeft="",htmlRight="",htmlC=""; var HL = '<li><a>...</a></li>'; html = '<li><a aria-label="Previous">首页</a></li>' for (var n = 0;n<5;n++){ htmlC += '<li '+((n-1)==0?'class="active"':'')+' page="'+(i+n-1)+'"><a>'+(i+n-1)+'</a></li>'; htmlLeft += '<li '+((n+2)==i?'class="active"':'')+' page="'+(n+2)+'"><a>'+(n+2)+'</a></li>'; htmlRight += '<li '+((set.num+n-5)==i?'class="active"':'')+' page="'+(set.num+n-5)+'"><a>'+(set.num+n-5)+'</a></li>'; } switch (type) { case "next": if(i<=4){ html+='<li page="1"><a>1</a></li>'+htmlLeft+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; }else if(i>=(set.num-3)){ html+='<li page="1"><a>1</a></li>'+HL+htmlRight+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; }else{ html += '<li page="1"><a>1</a></li>'+HL+htmlC+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; } break; case "prev": if(i<=4){ html+='<li page="1"><a>1</a></li>'+htmlLeft+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; }else if(i>=(set.num-3)){ html+='<li page="1"><a>1</a></li>'+HL+htmlRight+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; }else{ html += '<li page="1"><a>1</a></li>'+HL+htmlC+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; } break; case "首页" : html+='<li page="1"><a>1</a></li>'+htmlLeft+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; break; case "尾页" : html+='<li page="1"><a>1</a></li>'+HL+htmlRight+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; break; case "jump" : if(i<=4){ if(i==1){ html+='<li page="1"><a>1</a></li>'+htmlLeft+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; }else{ html+='<li page="1"><a>1</a></li>'+htmlLeft+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; } }else if((i>=set.num-3)&&(set.num>=7)){ if(i==set.num){ html+='<li page="1"><a>1</a></li>'+HL+htmlRight+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; }else{ html+='<li page="1"><a>1</a></li>'+HL+htmlRight+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; } }else{ html += '<li page="1"><a>1</a></li>'+HL+htmlC+HL+'<li page="'+set.num+'"><a>'+set.num+'</a></li>'; } } html += '<li><a aria-label="Next">尾页</a></li>'; if (this.num > 5 || this.num < 3) { set.elem.html(html); clickpages.init({num:set.num,elem:set.elem,callback:set.callback}); } } } if(set.num<=1){ $(".pagination").html(''); return; }else if(parseInt(set.num)<=6){ n = parseInt(set.num); var html='<li><a aria-label="Previous">首页</a></li>'; for(var i=1;i<=n;i++){ if(i==set.startnum){ html+='<li page="'+i+'"><a>'+i+'</a></li>'; }else{ html+='<li page="'+i+'"><a>'+i+'</a></li>'; } } html +='<li><a aria-label="Next">尾页</a></li>'; set.elem.html(html); clickpages.init(); }else{ clickpages.newPages("jump",set.startnum) } }

上面是 pager.js部分

html部分

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c18f52f103c9ae84ab8515e890b4be21.html