JS 自制換頁函數

function getli(index,count){   //index 是當前頁數   count是總頁數

    var li="<li><a href='#' aria-label='Previous' onclick='toup()'>
        <span aria-hidden='true'>«</span></a></li>";
        if(count<6){
            for (var i=0;i<count;i++){
                li+="<li><a href='#' >"+(i+1)+"</a></li>";
            }
        }else{
            var page=0;
            if(index-2<1){
                if(index-1<1){
                    page=index;
                }else{
                    page=index-1;
                }
            }else{
                page=index-2;
            }
            if(index+2>count){
                if(index+1>count){
                    count=index;
                }else{
                    count=index+1;
                }
            }else{
                if(index<3){
                    count=5
                }else{
                    count=index+2;
                }
            }
            if(count-page+1<5){
                var i=5-(count-page)-1;
                page=page-i;
            }

            for (var i=0;page<=count;page++){
                li+="<li><a href='#'>"+page+"</a></li>";
            }
        }
        li+="<li><a href='#' aria-label='Next' onclick='tonext()'><span aria-hidden='true'>»</span></a></li>";
        $("#listPage").html(li);
    }

效果圖
這裏寫圖片描述

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章