JavaScript小应用之分页算法
程序员文章站
2022-04-12 09:40:33
...
效果图:
测试:
-
http://www.frontsurf.com/company/news?page=2
-
function getPagination(curPage, pagesize, count) { var total = Math.ceil(count/pagesize); if (curPage > total) curPage = total; var pl = curPage; // pager left var cur_item = "<b>" + pl + "</b>"; var nl = 0; // number left while(--pl > 0){ cur_item = "<a>" + pl +"</a>" + cur_item; nl++; if(nl >= 2 && pl > 3){ cur_item = "<a>1</a>" + "<i>...</i>" + cur_item; break; } } var pr = curPage; // pager right var nr = 0; // number right var mcb = 5; // max continuously button while(++pr <= total){ cur_item = cur_item + "<a>" + pr +"</a>"; nr++; if( (1 + nl + nr) >= mcb && pr < (total -1 )){ cur_item = cur_item + "<i>...</i>" + "<a>" + total + "</a>"; break; } } return cur_item; } console.log(getPagination(1, 10, 8)); console.log(getPagination(1, 10, 18)); console.log(getPagination(4, 10, 188)); console.log(getPagination(8, 10, 188)); console.log(getPagination(17, 10, 188));
测试:
var curPage = 1, pagesize = 8, count = 38; // <b>1</b><a>2</a><a>3</a><a>4</a><a>5</a> var curPage = 2, pagesize = 8, count = 38; // <a>1</a><b>2</b><a>3</a><a>4</a><a>5</a> var curPage = 5, pagesize = 8, count = 38; // <a>1</a><a>2</a><a>3</a><a>4</a><b>5</b> var curPage = 5, pagesize = 8, count = 200; // <a>1</a><a>2</a><a>3</a><a>4</a><b>5</b><a>6</a><i>...</i><a>25</a> var curPage = 10, pagesize = 8, count = 200; // <a>1</a><i>...</i><a>8</a><a>9</a><b>10</b><a>11</a><a>12</a><i>...</i><a>25</a>
-
http://www.frontsurf.com/company/news?page=2
-