js分页函数

1970-01-0161次阅读javascript

js分页函数,仅供参考。。。

const curr_page = 1;//当前页数
const page = 10;//一页10条数据
const totalpage = Math.ceil(总条数/page);//总页数

function pageHtml(curr_page,totalpage,offset){

    if(totalpage < 2) return;

    if(!offset) offset=4;
    var from=curr_page-offset;
    var to=curr_page+offset;
    if(curr_page>(totalpage-offset)) from=totalpage-offset*2+1;
    if(from<1) from=1;
    if(curr_page<offset) to=offset*2;
    if(to>totalpage) to=totalpage;
    var pagehtml=new Array();
    if(curr_page>1) pagehtml.push('<a class="prev" href="#第'+(curr_page-1)+'页" target="_self" data-page="'+(curr_page-1)+'">&lt;</a>');
    if(curr_page-offset > 1 && from>1) {
        pagehtml.push('<a href="#第1页" target="_self" data-page="1">1</a>');
        if(from>2) pagehtml.push('<span>...</span>');
    }
    for(var i=from;i<=to;i++){
        if(i!=curr_page){
            pagehtml.push('<a href="#第'+i+'页" target="_self" data-page="'+i+'">'+i+'</a>');
        } else {
            pagehtml.push('<strong class="cur" title="当前第 '+curr_page+' 页">'+curr_page+'</strong>');
        }
    }
    if (totalpage > to) {
        if(totalpage-to>1) pagehtml.push('<span>...</span>');
        pagehtml.push('<a href="#第'+totalpage+'页" target="_self" data-page="'+totalpage+'">'+totalpage+'</a>');
    }
    if (totalpage > curr_page) pagehtml.push('<a class="next" href="#第'+(curr_page+1)+'页" target="_self" data-page="'+(curr_page+1)+'">&gt;</a>');
    return pagehtml.join('');
};

pageHtml(curr_page,totalpage);

 

上一篇: 没有了~下一篇: 记录一下Win7下IE11右下角Edge不可点解决方法  

js分页函数相关文章