首页 > 代码库 > 分页 js

分页 js

<script>
    $(function () {
        var barSize = 6,
                midBarSize = Math.floor(barSize / 2),
                totalPg = parseInt($("#totalPage").text()),
                curPgCode = parseInt($("#curPageCode").text()),
                item = "",
                i = 0;
        if (totalPg <= barSize) {//总页数小于等于10页,加载所有
            for (i; i < totalPg; i++) {
                if (curPgCode === i) {
                    item += "<li class=‘page-item curPageCode‘><a class=‘page-link‘ href=‘javascript:void()‘>" + i + "</a></li>";
                } else {
                    item += "<li class=‘page-item‘><a class=‘page-link‘ href=http://www.mamicode.com/‘${base}?page=" + i + "‘>" + i + "</a></li>";
                }
            }
            $("#page-bar-content").after(item);
        } else if (totalPg > barSize) {//总页数大于10页,加载10页
            if (curPgCode <= midBarSize) {//当前页为前6页,加载前10页
                for (i; i < barSize; i++) {
                    if (i === curPgCode) {
                        item += "<li class=‘page-item curPageCode‘><a class=‘page-link‘ href=‘javascript:void()‘>" + i + "</a></li>";
                    } else {
                        item += "<li class=‘page-item‘><a class=‘page-link‘ href=http://www.mamicode.com/‘${base}?page=" + i + "‘>" + i + "</a></li>";
                    }
                }
                $("#page-bar-content").after(item);
            } else if (curPgCode >= totalPg - midBarSize) {//当前页是后5页,加载最后10页
                for (i = totalPg - barSize; i <= totalPg - 1; i++) {
                    if (i === curPgCode) {
                        item += "<li class=‘page-item curPageCode‘><a class=‘page-link‘ href=‘javascript:void()‘>" + i + "</a></li>";
                    } else {
                        item += "<li class=‘page-item‘><a class=‘page-link‘ href=http://www.mamicode.com/‘${base}?page=" + i + "‘>" + i + "</a></li>";
                    }
                }
                $("#page-bar-content").after(item);
            } else {//当前页不是前几页,也不是最后几页.加载当前页的前5页和后4页
                for (i = curPgCode - midBarSize; i <= curPgCode + midBarSize - 1; i++) {
                    if (i === curPgCode) {
                        item += "<li class=‘page-item curPageCode‘><a class=‘page-link‘ href=‘javascript:void()‘>" + i + "</a></li>";
                    } else {
                        item += "<li class=‘page-item‘><a class=‘page-link‘ href=http://www.mamicode.com/‘${base}?page=" + i + "‘>" + i + "</a></li>";
                    }
                }
                $("#page-bar-content").after(item);
            }
        }

    });
</script>

 

分页 js