前提:頁碼最多顯示10條app
具體實現的貼吧分頁效果爲:框架
·當前頁碼>5,且總頁數>10時,保證頁面一直顯示10個頁碼。spa
·在此基礎上,當前頁+ 5 <= 尾頁時,保證當前頁碼一直位於第五個頁碼的位置。code
for(var i = 1; i <= pageCount; i++){ if(i == 1){ //添加分頁欄框架的開始標籤 pagingStr = '<div class="navigation"><ol class="wp-paginate"><li><span class="title">頁碼:</span></li>'; if(curPage != 1){ pagingStr += '<li><a href="#" onclick="getFilesInfoByFolderId('+folderId+','+i+')"><span>首頁</span></a></li>' } } var pageStartIndex = 1; var pageEndIndex = 1; //添加分頁欄框架的內容 if(curPage > 5 && pageCount > 10){//知足條件,則保證頁面一直顯示10個頁碼。 if(curPage + 5 >= pageCount){//知足條件,則保證當前頁碼一直位於第五個頁碼的位置上 pageStartIndex = pageCount - 9; pageEndIndex = pageCount; }else{ pageStartIndex = curPage - 4; pageEndIndex = curPage + 5; } if(i >= pageStartIndex && i <=pageEndIndex){ if(curPage == i){ pagingStr += '<li><a title="'+i+'" class="page current" >'+i+'</a></li>'; }else{ pagingStr += '<li><a href="#" title="'+i+'" class="page" onclick="getFilesInfoByFolderId('+folderId+','+i+')">'+i+'</a></li>'; } } }else{ if(i <= 10){ if(curPage == i){ pagingStr += '<li><a title="'+i+'" class="page current" >'+i+'</a></li>'; }else{ pagingStr += '<li><a href="#" title="'+i+'" class="page" onclick="getFilesInfoByFolderId('+folderId+','+i+')">'+i+'</a></li>'; } } } if(i == pageCount){ //添加分頁欄框架的結束標籤 if(curPage != pageCount){ pagingStr += '<li><a href="#" onclick="getFilesInfoByFolderId('+folderId+','+i+')"><span>尾頁</span></a></li>' } pagingStr += '</ol></div>'; } } $('.navigation').remove(); $('#rigthContentBox').append(pagingStr); }