jq分頁插件(jquery.pagination.js)只有上一頁下一頁和跳轉


引入jq再引jquery.pagination.js

用法:粘貼html js css 引入jq jquery.pagination.js(代碼在最下面)

html:

<div id="pagingmix" class="page m-style M-box3">
            
</div>    

js:

//分頁
function laodPage(pages){
    $('.M-box3').pagination({
        pageCount: pages,
        jump: true,
        coping: true,
        homePage: '首頁',
        endPage: '末頁',
        prevContent: '上頁',
        nextContent: '下頁',
        callback: function (api) {
           // loadDcotorMaintin(api.getCurrent());//回調函數
            console.log(api);
        }
    });
    
}    

 

 

css:

/*分頁  */
.M-box3{
    display: flex;
    justify-content: center;
    margin:20px 0;
}
.m-style:before,
.m-style:after {
    content: "";
    display: table;
}

.m-style:after {
    clear: both;
    overflow: hidden;
}
.m-style span{
    display:block;
    padding:0 15px;
    border:1px solid #00A09E;
    margin:0 5px;
    line-height:35px;
}
.m-style .active{
    display:block;
    padding:0 15px;
    border:1px solid #00A09E;
    margin:0 5px;
    line-height:35px;
    background:#00A09E;
    color: white !important;
}
.m-style a{
    display:block;
    padding:0 15px;
    border:1px solid #eee;
    margin:0 5px;
    text-align:center;
    line-height:35px;
    font-color:white !important;
}
.m-style a:hover,.m-style .active:hover a{
    border:1px solid #00A09E;
    text-decoration:none;
}
#bp-3-element-test{
    display: flex;
    flex-direction: row;
}
 #bp-3-element-test>li{
    line-height: 30px;
    margin:0 5px;
}

.jump-ipt {
    float: left;
    margin: 0 5px;
    width: 38px;
    height: 38px;
    line-height: 38px;
    text-align: center;
    background: #fff;
    border: 1px solid #ebebeb;
    outline: none;
    color: #bdbdbd;
    font-size: 14px;
}

 

jquery.pagination.js:

/**
 * pagination.js 1.5.1
 * A jQuery plugin to provide simple yet fully customisable pagination.
 * @version 1.5.1
 * @author mss
 * @url https://github.com/Maxiaoxiang/jQuery-plugins
 *
 * @調用方法
 * $(selector).pagination(option, callback);
 * -此處callback是初始化調用,option里的callback是點擊頁碼后調用
 * 
 * -- example --
 * $(selector).pagination({
 *     ... // 配置參數
 *     callback: function(api) {
 *         console.log('點擊頁碼調用該回調'); //切換頁碼時執行一次回調
 *     }
 * }, function(){
 *     console.log('初始化'); //插件初始化時調用該方法,比如請求第一次接口來初始化分頁配置
 * });
 */
;
(function (factory) {
    if (typeof define === "function" && (define.amd || define.cmd) && !jQuery) {
        // AMD或CMD
        define(["jquery"], factory);
    } else if (typeof module === 'object' && module.exports) {
        // Node/CommonJS
        module.exports = function (root, jQuery) {
            if (jQuery === undefined) {
                if (typeof window !== 'undefined') {
                    jQuery = require('jquery');
                } else {
                    jQuery = require('jquery')(root);
                }
            }
            factory(jQuery);
            return jQuery;
        };
    } else {
        //Browser globals
        factory(jQuery);
    }
}(function ($) {

    //配置參數
    var defaults = {
        totalData: 0, //數據總條數
        showData: 0, //每頁顯示的條數
        pageCount: 9, //總頁數,默認為9
        current: 1, //當前第幾頁
        prevCls: 'prev', //上一頁class
        nextCls: 'next', //下一頁class
        prevContent: '<', //上一頁內容
        nextContent: '>', //下一頁內容
        activeCls: 'active', //當前頁選中狀態
        coping: false, //首頁和尾頁
        isHide: false, //當前頁數為0頁或者1頁時不顯示分頁
        homePage: '', //首頁節點內容
        endPage: '', //尾頁節點內容
        keepShowPN: false, //是否一直顯示上一頁下一頁
        mode: 'unfixed', //分頁模式,unfixed:不固定頁碼數量,fixed:固定頁碼數量
        count: 4, //mode為unfixed時顯示當前選中頁前后頁數,mode為fixed顯示頁碼總數
        jump: false, //跳轉到指定頁數
        jumpIptCls: 'jump-ipt', //文本框內容
        jumpBtnCls: 'jump-btn', //跳轉按鈕
        jumpBtn: '跳轉', //跳轉按鈕文本
        callback: function () {} //回調
    };

    var Pagination = function (element, options) {
        //全局變量
        var opts = options, //配置
            current, //當前頁
            $document = $(document),
            $obj = $(element); //容器

        /**
         * 設置總頁數
         * @param {int} page 頁碼
         * @return opts.pageCount 總頁數配置
         */
        this.setPageCount = function (page) {
            return opts.pageCount = page;
        };

        /**
         * 獲取總頁數
         * 如果配置了總條數和每頁顯示條數,將會自動計算總頁數並略過總頁數配置,反之
         * @return {int} 總頁數
         */
        this.getPageCount = function () {
            return opts.totalData && opts.showData ? Math.ceil(parseInt(opts.totalData) / opts.showData) : opts.pageCount;
        };

        /**
         * 獲取當前頁
         * @return {int} 當前頁碼
         */
        this.getCurrent = function () {
            return current;
        };

        /**
         * 填充數據
         * @param {int} 頁碼
         */
        this.filling = function (index) {
            var html = '';
            current = parseInt(index) || parseInt(opts.current); //當前頁碼
            var pageCount = this.getPageCount(); //獲取的總頁數
            switch (opts.mode) { //配置模式
                case 'fixed': //固定按鈕模式
                    html += '<a href="javascript:;" class="' + opts.prevCls + '">' + opts.prevContent + '</a>';
                    if (opts.coping) {
                        var home = opts.coping && opts.homePage ? opts.homePage : '1';
                        html += '<a href="javascript:;" data-page="1">' + home + '</a>';
                    }
                    var start = current > opts.count - 1 ? current + opts.count - 1 > pageCount ? current - (opts.count - (pageCount - current)) : current - 2 : 1;
                    var end = current + opts.count - 1 > pageCount ? pageCount : start + opts.count;
                    for (; start <= end; start++) {
                        if (start != current) {
                            html += '<a href="javascript:;" data-page="' + start + '">' + start + '</a>';
                        } else {
                            html += '<span class="' + opts.activeCls + '">' + start + '</span>';
                        }
                    }
                    if (opts.coping) {
                        var _end = opts.coping && opts.endPage ? opts.endPage : pageCount;
                        html += '<a href="javascript:;" data-page="' + pageCount + '">' + _end + '</a>';
                    }
                    html += '<a href="javascript:;" class="' + opts.nextCls + '">' + opts.nextContent + '</a>';
                    break;
                case 'unfixed': //不固定按鈕模式
                    if (opts.keepShowPN || current > 1) { //上一頁
                        html += '<a href="javascript:;" class="' + opts.prevCls + '">' + opts.prevContent + '</a>';
                    } else {
                        if (opts.keepShowPN == false) {
                            $obj.find('.' + opts.prevCls) && $obj.find('.' + opts.prevCls).remove();
                        }
                    }
                    if (current >= opts.count + 2 && current != 1 && pageCount != opts.count) {
                        var home = opts.coping && opts.homePage ? opts.homePage : '1';
                        html += opts.coping ? '<a href="javascript:;" data-page="1">' + home + '</a><span>...</span>' : '';
                    }
                    var start = (current - opts.count) <= 1 ? 1 : (current - opts.count);
                    var end = (current + opts.count) >= pageCount ? pageCount : (current + opts.count);
                    for (; start <= end; start++) {
                        if (start <= pageCount && start >= 1) {
                            if (start != current) {
                                html += '<a href="javascript:;" data-page="' + start + '">' + start + '</a>';
                            } else {
                                html += '<span class="' + opts.activeCls + '">' + start + '</span>';
                            }
                        }
                    }
                    if (current + opts.count < pageCount && current >= 1 && pageCount > opts.count) {
                        var end = opts.coping && opts.endPage ? opts.endPage : pageCount;
                        html += opts.coping ? '<span>...</span><a href="javascript:;" data-page="' + pageCount + '">' + end + '</a>' : '';
                    }
                    if (opts.keepShowPN || current < pageCount) { //下一頁
                        html += '<a href="javascript:;" class="' + opts.nextCls + '">' + opts.nextContent + '</a>';
                    } else {
                        if (opts.keepShowPN == false) {
                            $obj.find('.' + opts.nextCls) && $obj.find('.' + opts.nextCls).remove();
                        }
                    }
                    break;
                case 'easy': //簡單模式
                    break;
                default:
            }
            html += opts.jump ? '<input type="text" class="' + opts.jumpIptCls + '"><a href="javascript:;" class="' + opts.jumpBtnCls + '">' + opts.jumpBtn + '</a>' : '';
            $obj.empty().html(html);
        };

        //綁定事件
        this.eventBind = function () {
            var that = this;
            var pageCount = that.getPageCount(); //總頁數
            var index = 1;
            $obj.off().on('click', 'a', function () {
                if ($(this).hasClass(opts.nextCls)) {
                    if ($obj.find('.' + opts.activeCls).text() >= pageCount) {
                        $(this).addClass('disabled');
                        return false;
                    } else {
                        index = parseInt($obj.find('.' + opts.activeCls).text()) + 1;
                    }
                } else if ($(this).hasClass(opts.prevCls)) {
                    if ($obj.find('.' + opts.activeCls).text() <= 1) {
                        $(this).addClass('disabled');
                        return false;
                    } else {
                        index = parseInt($obj.find('.' + opts.activeCls).text()) - 1;
                    }
                } else if ($(this).hasClass(opts.jumpBtnCls)) {
                    if ($obj.find('.' + opts.jumpIptCls).val() !== '') {
                        index = parseInt($obj.find('.' + opts.jumpIptCls).val());
                    } else {
                        return;
                    }
                } else {
                    index = parseInt($(this).data('page'));
                }
                that.filling(index);
                typeof opts.callback === 'function' && opts.callback(that);
            });
            //輸入跳轉的頁碼
            $obj.on('input propertychange', '.' + opts.jumpIptCls, function () {
                var $this = $(this);
                var val = $this.val();
                var reg = /[^\d]/g;
                if (reg.test(val)) $this.val(val.replace(reg, ''));
                (parseInt(val) > pageCount) && $this.val(pageCount);
                if (parseInt(val) === 0) $this.val(1); //最小值為1
            });
            //回車跳轉指定頁碼
            $document.keydown(function (e) {
                if (e.keyCode == 13 && $obj.find('.' + opts.jumpIptCls).val()) {
                    var index = parseInt($obj.find('.' + opts.jumpIptCls).val());
                    that.filling(index);
                    typeof opts.callback === 'function' && opts.callback(that);
                }
            });
        };

        //初始化
        this.init = function () {
            this.filling(opts.current);
            this.eventBind();
            if (opts.isHide && this.getPageCount() == '1' || this.getPageCount() == '0') {
                $obj.hide();
            } else {
                $obj.show();
            }
        };
        this.init();
    };

    $.fn.pagination = function (parameter, callback) {
        if (typeof parameter == 'function') { //重載
            callback = parameter;
            parameter = {};
        } else {
            parameter = parameter || {};
            callback = callback || function () {};
        }
        var options = $.extend({}, defaults, parameter);
        return this.each(function () {
            var pagination = new Pagination(this, options);
            callback(pagination);
        });
    };

}));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM