用pageGroup.js实现分页功能
1.html页面中
引入
<link rel="stylesheet" type="text/css" href="/stylesheets/pageGroup.css"> <script src="/javascripts/web/pageGroup.js" type="text/javascript"></script>
<div id="pageGro" class="cb"> {{if maxPage>0}}//总页数 <div class="pageUp">上一页</div> <div class="pageList"> <ul> </ul> </div> <div class="pageDown">下一页</div>{{/if}} <div id="countPage" style="display:none">{{maxPage}}</div>//总页数 <div id="nowPage" style="display:none">{{page}}</div>//当前页 <div id="title" style="display:none">{{title}}</div>//标题 </div>
2.pageGroup.css
/* CSS Document */ /*分页*/ #pageGro{ width:400px; height:25px; margin:0px auto; padding-top:30px;} #pageGro div,#pageGro div ul li{ font-size:12px; color:#999; line-height:23px; float:left; margin-left:5px;} #pageGro div ul li{ width:22px; text-align:center; border:1px solid #999; cursor:pointer;} #pageGro div ul li.on{ color:#fff; background:#3c90d9; border:1px solid #3c90d9;} #pageGro .pageUp,#pageGro .pageDown{ width:63px; border:1px solid #999; cursor:pointer;} #pageGro .pageUp{ text-indent:23px; background:url(/images/pageUp.png) 5px 7px no-repeat;} #pageGro .pageDown{ text-indent:5px; background:url(/images/pageDown.png) 46px 6px no-repeat;}
3.pageGroup.js
// JavaScript Document $(function(){ //根据总页数判断,如果小于5页,则显示所有页数,如果大于5页,则显示5页。根据当前点击的页数生成 var maxPage=jQuery("#countPage").text(); var nowPage=jQuery("#nowPage").text(); var index=parseInt(nowPage);//当前页 var title=jQuery("#title").text(); var pageCount =parseInt(maxPage) ;//模拟后台总页数 //生成分页按钮 if(pageCount>5){ page_icon(1,5,0); pageGroup(index,pageCount); }else{ //alert("66"); page_icon(1,pageCount,0); pageGroup(index,pageCount); } //点击分页按钮触发 $("#pageGro li").live("click",function(){ if(pageCount > 5){ var pageNum = parseInt($(this).html());//获取当前页数 window.location.href = "/article/search/"+title+"/"+pageNum; pageGroup(pageNum,pageCount); }else{ var indexpage=jQuery(this).text(); window.location.href = "/article/search/"+title+"/"+indexpage; $(this).addClass("on"); $(this).siblings("li").removeClass("on"); } }); //点击上一页触发 $("#pageGro .pageUp").click(function(){ if(pageCount > 5){ if(index>1){ window.location.href = "/article/search/"+title+"/"+(index-1); } }else{ if(index > 1){ window.location.href = "/article/search/"+title+"/"+(index-1); $("#pageGro li").removeClass("on");//清除所有选中 } } }); //点击下一页触发 $("#pageGro .pageDown").click(function(){ if(pageCount > 5){ if(nowPage<pageCount){ window.location.href = "/article/search/"+title+"/"+(index+1); } }else{ if(index< pageCount){ window.location.href = "/article/search/"+title+"/"+(index+1); $("#pageGro li").removeClass("on");//清除所有选中 } } }); }); //点击跳转页面 function pageGroup(pageNum,pageCount){ if(pageCount>5){ switch(pageNum){ case 1: page_icon(1,5,0); break; case 2: page_icon(1,5,1); break; case pageCount-1: page_icon(pageCount-4,pageCount,3); break; case pageCount: page_icon(pageCount-4,pageCount,4); break; default: page_icon(pageNum-2,pageNum+2,2); break; } } if(pageCount<5){ switch(pageNum){ case 1: page_icon(1,pageCount,0); break; case 2: page_icon(1,pageCount,1); break; case pageCount-1: page_icon(1,pageCount,2); break; case pageCount: page_icon(1,pageCount,3); break; } } if(pageCount==5){ switch(pageNum){ case 1: page_icon(1,pageCount,0); break; case 2: page_icon(1,pageCount,1); break; case pageCount-1: page_icon(1,pageCount,3); break; case pageCount: page_icon(1,pageCount,4); break; default: page_icon(1,pageNum+2,2); break; } } } //根据当前选中页生成页面点击按钮 function page_icon(page,count,eq){ var ul_html = ""; for(var i=page; i<=count; i++){ ul_html += "<li>"+i+"</li>"; } $("#pageGro ul").html(ul_html); $("#pageGro ul li").eq(eq).addClass("on"); } //上一页 function pageUp(pageNum,pageCount){ switch(pageNum){ case 1: break; case 2: page_icon(1,5,0); break; case pageCount-1: page_icon(pageCount-4,pageCount,2); break; case pageCount: page_icon(pageCount-4,pageCount,3); break; default: page_icon(pageNum-2,pageNum+2,1); break; } } //下一页 function pageDown(pageNum,pageCount){ switch(pageNum){ case 1: page_icon(1,5,1); break; case 2: page_icon(1,5,2); break; case pageCount-1: page_icon(pageCount-4,pageCount,4); break; case pageCount: break; default: page_icon(pageNum-2,pageNum+2,3); break; } }