自定义分页栏(支持jquery)

自定义分页栏

使用介绍
步骤1、在css样式文件中添加分页栏样式(可按需调整)
步骤2、在对应的html页面添加分页栏容器
步骤3、新建js文件并引用(分页js继承jquery,需要jquery支持)
步骤4、初始化分页

效果图如下:

自定义分页栏展示

详情代码如下:

代码块

步骤1、在css样式文件中添加分页栏样式(可按需调整)
/**分页css start **/  
.g-pagerwp{ height:34px; line-height:34px; margin: 20px;}  
.g-pager{ text-align:center; color: #111111;}  
.g-pagerwp .g-btn{ vertical-align:top}  
.g-cf:after {clear: both;content: "";display: table;}  
.g-cf {zoom:1;}  
.g-pager a,.g-pager input{ cursor:pointer; border:solid 1px #0F71BE;padding: 5px 15px; color:#0F71BE; margin:0 5px; vertical-align:middle; }  
.g-pager a.cur,.g-pager a:hover{ background-color:#0F71BE; color:#fff}  
.g-pager a.no{ border-color:#A3A3A3; color:#A3A3A3; background-color:#E4F2F9}  
.g-pager span{ margin-right:10px; }  
.g-pager input{ cursor:default; width:36px; padding:8px; }  
/** 分页css end **/  

步骤2、在对应的html页面添加分页栏容器
<!-- 分页栏容器 start -->
<div class="content">
    <div class="g-cf g-pagerwp">  
        <div style="visibility:hidden" class="g-pager">
        </div>  
    </div>  
</div>
<!-- 分页栏容器 end -->

步骤3、新建js文件并引用(分页js继承jquery,需要jquery支持)
/************ 分页js继承jquery *************/  
$.fn.pageBar = function(options) {  
    var configs = {  
        PageIndex: 1,  //设置当前页码  
        PageSize: 20,  //设置分页大小  
        TotalPage: 0,   //设置总页数  
        RecordCount: 0,  //设置数据总数  
        showPageCount: 4,   //设置连续页数的个数
        onPageClick: function(pageIndex) {  
            return false;   //默认的翻页事件  
        }  
    }  
    $.extend(configs, options)
    var tmp = "",  
    i = 0,  
    j = 0,  
    a = 0,  
    b = 0,  
    totalpage = parseInt(configs.RecordCount / configs.PageSize);  
    totalpage = configs.RecordCount % configs.PageSize > 0 ? totalpage + 1 : totalpage;  
    tmp += "<span>总计:" + configs.RecordCount + "</span > ";  
    tmp += " <span>页数:" + totalpage + "</span>";  
    if (configs.PageIndex > 1) {  
        tmp += "<a>上一页</a>"  
    } else {  
        tmp += "<a class=\"no\">上一页</a>"  
    }  
    tmp += "<a>1</a>";  
    if (totalpage > configs.showPageCount + 1) {  
        if (configs.PageIndex <= configs.showPageCount) {  
            i = 2;  
            j = i + configs.showPageCount;  
            a = 1;  
        } else if (configs.PageIndex > totalpage - configs.showPageCount) {  
            i = totalpage - configs.showPageCount;  
            j = totalpage;  
            b = 1;  
        } else {  
            var k = parseInt((configs.showPageCount - 1) / 2);  
            i = configs.PageIndex - k;  
            j = configs.PageIndex + k + 1;  
            a = 1;  
            b = 1;  
            if ((configs.showPageCount - 1) % 2) {  
                i -= 1  
            }  
        }  
    }  
    else {  
        i = 2;  
        j = totalpage;  
    }  
    if (b) {  
        tmp += "..."  
    }  
    for (; i < j; i++) {  
        tmp += "<a>" + i + "</a>"  
    }  
    if (a) {  
        tmp += " ... "  
    }  
    if (totalpage > 1) {  
        tmp += "<a>" + totalpage + "</a>"  
    }  
    if (configs.PageIndex < totalpage) {  
        tmp += "<a>下一页</a>"  
    } else {  
        tmp += "<a class=\"no\">下一页</a>"  
    }  
    tmp += "<input type=\"text\" /><a>GO</a>";
    var pager = this.html(tmp);  
    var index = pager.children('input')[0];
    pager.children('a').click(function() {  
        var cls = $(this).attr('class');  
        if (this.innerHTML == '上一页') {  
            if (cls != 'no') {  
                configs.onPageClick(configs.PageIndex - 2)  
            }  
        } else if (this.innerHTML == '下一页') {  
            if (cls != 'no') {  
                configs.onPageClick(configs.PageIndex)  
            }  
        } else if (this.innerHTML == 'GO') {  
            if (!isNaN(index.value) && index.value != "") {  
                var indexvalue = parseInt(index.value);  
                indexvalue = indexvalue < 1 ? 1 : indexvalue  
                indexvalue = indexvalue > totalpage ? totalpage : indexvalue  
                configs.onPageClick(indexvalue - 1)  
            }  
        } else {  
            if (cls != 'cur') {  
                configs.onPageClick(parseInt(this.innerHTML) - 1)  
            }  
        }  
    }).each(function() {  
        if (configs.PageIndex == parseInt(this.innerHTML)) {  
            $(this).addClass('cur')  
        }  
    })  
}   

步骤4、初始化分页
//初始化分页
function initPage(recodeCount,pageIndex,totalPage){
    //设置分页信息  
    var pageOptions = {
        AllowPaging: true,  
        PageIndex: pageIndex,       //设置当前页码
        PageSize: 15,               //设置分页大小
        RecordCount: recodeCount,   //设置数据总数
        TotalPage: totalPage,  //设置总页数
        showPageCount: 3,           //设置连续页数的个数
        onPageClick: function(pageIndex) {  
            //alert("您点击了第" + parseInt(pageIndex + 1) + "页");    //自定义您的翻页事件  
            
            var type = window.location.search.substr(7);
            if(type == "news-page"){
                getNewsList(pageIndex);
            }else if(type == "dongTai-page"){
                getDongTaiList(pageIndex);
            }else if(type == "zhiDu-page"){
                getZhiDuList(pageIndex);
            }else if(type == "yuanDi-page"){
                getYuaDiList(pageIndex);
            }else if(type == "tongZhi-page"){
                getNoticeList(pageIndex);
            }
            return false;  
        }  
    }  
    //初始化分页栏  
    $('.g-pagerwp .g-pager').css('visibility', 'visible').pageBar(pageOptions);   
}

完。
如果你看到了这里,觉得文章写得不错就给个赞, 关注公众号,可订阅更多干货?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢!
公众号:学软件开发就这么简单

发布了19 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lixibang/article/details/78914890
今日推荐