欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JavaScript小应用之分页算法

程序员文章站 2022-04-12 09:40:33
...
效果图:

JavaScript小应用之分页算法
            
    
    博客分类: javaScript javascript分页插件 



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

-
  • JavaScript小应用之分页算法
            
    
    博客分类: javaScript javascript分页插件 
  • 大小: 2.4 KB