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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> .pagination{vertical-align:middle;} .pagination a,.pagination span{padding:0 5px;margin-right:5px;border: 1px solid .pagination .pageem{line-height:32px;display:inline-block;vertical-align:middle;font-style:normal;color: .pagination .pageem1{margin-left:2em;margin-right:4px;} .pagination .pageem2{margin-left:5px;} .pagination a {text-decoration: none;} .pagination a:hover,.pagination a:active {border-color: .pagination .pagefirst,.pagination .pageprev,.pagination .pagelast,.pagination .pagenext{font-size:12px;} .pagination .pageprev{margin-right:2em;} .pagination .pagenext{margin-left:2em;} .pagination .current {border-color: .pagination .disabled {border: 1px solid .pagination .pagepoints{border-color:transparent;} .pagination .pageselect{box-sizing:border-box;height:30px;vertical-align:middle;} </style> </head> <body>
<div id="a"></div>
<script> /* * -- getPagination -- * return: pagesHtml stringObject * params: * totalItems: [number] 总条数 (必选) * opts : {...} 配置项对象(可选) * |----- currentPage: [number] [default:1] //当前页 * |----- pageSize: [number] [default:10] //每页条数 * |----- adjacents: [number] [default:2] //折叠的前后的展示个数 * |----- ifNoLink: [boolean] [default:false] //是否禁用链接 * |----- ifShowSelect: [boolean] [default:false] //是否显示下拉列表 * |----- ifShowPrevNext: [boolean] [default:true] //是否显示上一页、下一页按钮 * |----- ifShowFirstLast: [boolean] [default:true] //是否显示首页、尾页按钮 * |----- ellipsis: [string] [default:'...'] //有折叠时,折叠的文字 * |----- targetPageUrlPart: [string] [default:'?foo=bar'] //分页链接的前半部分 * |----- wordPrev: [string] [default:'< 上一页'] //上一页按钮的文字 * |----- wordNext: [string] [default:'下一页 >'] //下一页按钮的文字 * |----- wordFirst: [string] [default:'|<< 首 页'] //首页按钮的文字 * |----- wordLast: [string] [default:'末 页 >>|'] //尾页按钮的文字 * |----- whenLteOnePageIfReturnEmpty: [boolean] [default:false] //当小于等于1页时,是否返回空(否则返回分页html) * use: * var pageHtml= getPagination(563); * var pageHtml= getPagination(563,{ currentPage:12 }); * var pageHtml= getPagination(563,{ currentPage:12, wordPrev:'上一页' }); * ele.innerHTML= pageHtml; * console.log(pageHtml.totalPage); * console.log(pageHtml.selectHtml); * */ function getPagination(totalItems,opts){
function href(page){ return def.ifNoLink?'javascript:;':def.targetPageUrlPart+'&page='+page; } if(isNaN(totalItems)) return ''; totalItems=Math.abs(+totalItems); var def={ currentPage:1, pageSize:10, adjacents:2, ifNoLink:false, whenLteOnePageIfReturnEmpty:false, ifShowSelect:false, ifShowPrevNext:true, ifShowFirstLast:true, ellipsis:'...', targetPageUrlPart:'?foo=bar', wordPrev:'< 上一页', wordNext:'下一页 >', wordFirst:'|<< 首 页', wordLast: '末 页 >>|' }; if(opts){ for(var i in opts) def[i]=opts[i]; }
var prev= def.currentPage - 1, next = def.currentPage + 1, lastpage = Math.ceil(totalItems/def.pageSize), lpm1 = lastpage - 1, pagination = '', selects, counter, pts='<span class="pagepoints">'+def.ellipsis+'</span>';
selects='<em class="pageem pageem1">跳到第</em><select class="pageselect" onchange="location.href=\''+def.targetPageUrlPart+'&page=\'+this.value;">'; for(counter =1; counter<=lastpage; counter++) selects += '<option value="'+counter+'" '+(counter === def.currentPage?' selected':'')+'>'+counter+'</span>'; selects += '</select><em class="pageem pageem2">页</em>'; lastpage<1 && (selects='', lastpage=1);
if(lastpage > 1){ pagination += '<div class="pagination">'; //first and previous button if(def.currentPage >1 ) { def.ifShowFirstLast && (pagination += '<a class="pagefirst" data-page="1" href="'+href(1)+'">'+def.wordFirst+'</a>'), def.ifShowPrevNext && (pagination += '<a class="pageprev" data-page="'+prev+'" href="'+href(prev)+'">'+def.wordPrev+'</a>'); }else{ def.ifShowFirstLast && (pagination += '<span class="pagefirst disabled">'+def.wordFirst+'</span>'), def.ifShowPrevNext && (pagination += '<span class="pageprev disabled">'+def.wordPrev+'</span>'); } //pages if(lastpage < 7 + (def.adjacents * 2)) { for (counter = 1; counter <= lastpage; counter++) { if (counter === def.currentPage) pagination += '<span class="current">' + counter + '</span>'; else pagination += '<a data-page="'+counter+'" href="' +href(counter)+ '">' + counter + '</a>'; } //enough pages to hide some }else if(lastpage >= 7 + (def.adjacents * 2)){ //close to beginning only hide later pages if(def.currentPage < 1 + (def.adjacents * 3)){ for(counter =1 ; counter<=(4 + (def.adjacents * 2))-1; counter++){ if(counter === def.currentPage) pagination += '<span class="current">'+counter+'</span>'; else pagination += '<a data-page="'+counter+'" href="'+href(counter)+'">'+counter+'</a>'; } pagination += pts, pagination += '<a data-page="'+lpm1+'" href="'+href(lpm1)+'">'+lpm1+'</a>', pagination += '<a data-page="'+lastpage+'" href="'+href(lastpage)+'">'+lastpage+'</a>'; //in middle hide some front and some back }else if(lastpage - (def.adjacents * 2) > def.currentPage && def.currentPage > (def.adjacents * 2)){ pagination += '<a data-page="1" href="'+href(1)+'">1</a>', pagination += '<a data-page="2" href="'+href(2)+'">2</a>', pagination += pts; for(counter =def.currentPage - def.adjacents;counter<=def.currentPage + def.adjacents;counter++){ if(counter===def.currentPage) pagination += '<span class="current">'+counter+'</span>'; else pagination += '<a data-page="'+counter+'" href="'+href(counter)+'">'+counter+'</a>'; } pagination += pts, pagination += '<a data-page="'+lpm1+'" href="'+href(lpm1)+'">'+lpm1+'</a>', pagination += '<a data-page="'+lastpage+'" href="'+href(lastpage)+'">'+lastpage+'</a>'; //close to end only hide early pages }else{ pagination += '<a data-page="1" href="'+href(1)+'">1</a>', pagination += '<a data-page="2" href="'+href(2)+'">2</a>', pagination += pts; for(counter = (lastpage - (1 + (def.adjacents * 3))); counter<=lastpage; counter++){ if(counter === def.currentPage) pagination += '<span class="current">'+counter+'</span>'; else pagination += '<a data-page="'+counter+'" href="'+href(counter)+'">'+counter+'</a>'; } } } //last and next button if(def.currentPage < lastpage){ def.ifShowPrevNext && (pagination += '<a class="pagenext" data-page="'+next+'" href="'+href(next)+'">'+def.wordNext+'</a>'); def.ifShowFirstLast && (pagination += '<a class="pagelast" data-page="'+lastpage+'" href="'+href(lastpage)+'">'+def.wordLast+'</a>'); }else{ def.ifShowPrevNext && (pagination += '<span class="pagenext disabled">'+def.wordNext+'</span>'); def.ifShowFirstLast && (pagination += '<span class="pagelast disabled">'+def.wordLast+'</span>'); }
def.ifShowSelect && (pagination += selects);
pagination += '</div>'; } else if(lastpage===1){ if (!def.whenLteOnePageIfReturnEmpty) { pagination += '<div class="pagination">'; def.ifShowFirstLast && (pagination += '<span class="pagefirst disabled">' + def.wordFirst + '</span>'); def.ifShowPrevNext && (pagination += '<span class="pageprev disabled">' + def.wordPrev + '</span>'); pagination += '<span class="current">1</span>'; def.ifShowPrevNext && (pagination += '<span class="pagenext disabled">' + def.wordNext + '</span>'); def.ifShowFirstLast && (pagination += '<span class="pagelast disabled">' + def.wordLast + '</span>'); pagination += '</div>'; } } var ret=new String(pagination); ret.totalPage=lastpage; ret.selectHtml=selects; ret.currentPage=def.currentPage; ret.pageSize=def.pageSize; ret.totalItems=totalItems; return ret; }
var pages=getPagination(20,{currentPage:1,ifShowSelect:true}); //console.log(''+pages); //console.log(pages.totalPage); //console.log(pages.selectHtml); document.getElementById('a').innerHTML=pages;
</script>
</body> </html>
|