jQuery插件--分頁

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <style>
        /* 外面盒子樣式---自己定義 */
        .page_div{margin:20px 10px 20px 0;color:#666}
        /* 頁數按鈕樣式 */
        .page_div button{display:inline-block;min-width:30px;height:28px;cursor:pointer;color:#666;font-size:13px;line-height:28px;background-color:#f9f9f9;border:1px solid #dce0e0;text-align:center;margin:0 4px;-webkit-appearance: none;-moz-appearance: none;appearance: none;}
        #firstPage,#lastPage,#nextPage,#prePage{width:50px;color:#0073A9;border:1px solid #0073A9}
        #nextPage,#prePage{width:70px}
        .page_div .current{background-color:#0073A9;border-color:#0073A9;color:#FFF}
        /* 頁面數量 */
        .totalPages{margin:0 10px}
        .totalPages span,.totalSize span{color:#0073A9;margin:0 5px}
        /*button禁用*/
        .page_div button:disabled{opacity:.5;cursor:no-drop}
    </style>
</head>

<body ontouchstart="" onmousemove="">
<div value="1 0"></div>
<div id="page" class="page_div"></div>
</body>
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/page1Me.js"></script>
<script>
    $("#page").paging({
        // pageNo: 18,
        // totalPage: 20,
        // totalSize: 300,
        pageNum: 5,
        totalNum: 14,
        totalList: 300,
        callback: function (num) {
            console.log(num);
        }
    });
    // 模擬ajax數據用以下方法,方便用戶更好的掌握用法
    // 參數爲當前頁
    // ajaxTest(1);
    // function ajaxTest(num) {
    //     $.ajax({
    //         url: "table.json",
    //         type: "get",
    //         data: {},
    //         dataType: "json",
    //         success: function(data) {
    //             console.log(data);
    //             //分頁
    //             $("#page").paging({
    //                 pageNo: num,
    //                 totalPage: data.totalPage,
    //                 totalSize: data.totalSize,
    //                 callback: function(num) {
    //                     ajaxTest(num)
    //                 }
    //             })
    //         }
    //     })
    // }
</script>
</html>

JS

;(function ($, window, document, undefined) {
    'use strict';
    function Paging(element, options) {
        this.element = element;
        this.options = {
            pageNum: options.pageNum || 1, // 當前頁碼
            totalNum: options.totalNum, // 總頁碼
            totalList: options.totalList, // 數據總記錄
            callback: options.callback // 回調函數
        };
        this.init();
    }
    Paging.prototype = {
        constructor: Paging,
        init: function () {
            this.createHtml();
            this.bindEvent();
        },
        createHtml: function () {
            var me = this;
            var content = [];
            var pageNum = me.options.pageNum;
            var totalNum = me.options.totalNum;
            var totalList = me.options.totalList;
            content.push("<button type='button' id='firstPage'>首頁</button><button type='button' id='prePage'>上一頁</button>");
            // 總頁數大於6必顯示省略號
            if (totalNum > 6) {
                // 1、當前頁碼小於5且總頁碼大於6 省略號顯示後面+總頁碼
                if (pageNum < 5) {
                    // 1與6主要看要顯示多少個按鈕 目前都顯示5個
                    for (var i = 1; i < 6; i++) {
                        if (pageNum !== i) {
                            content.push("<button type='button'>" + i + "</button>");
                        } else {
                            content.push("<button type='button' class='current'>" + i + "</button>");
                        }
                    }
                    content.push(". . .");
                    content.push("<button type='button'>" + totalNum + "</button>");
                } else {
                    // 2、當前頁碼接近後面 中間隔3個 省略號顯示後面+總頁面
                    if (pageNum < totalNum - 3) {
                        for (var i = pageNum - 2; i < pageNum + 3; i++) {
                            if (pageNum !== i) {
                                content.push("<button type='button'>" + i + "</button>");
                            } else {
                                content.push("<button type='button' class='current'>" + i + "</button>");
                            }
                        }
                        content.push(". . .");
                        content.push("<button type='button'>" + totalNum + "</button>");
                    } else {
                        // 3、頁碼至少在5,最多在【totalNum - 3】的中間位置 第一頁+省略號顯示前面
                        content.push("<button type='button'>" + 1 + "</button>");
                        content.push(". . .");
                        for (var i = totalNum - 4; i < totalNum + 1; i++) {
                            if (pageNum !== i) {
                                content.push("<button type='button'>" + i + "</button>");
                            } else {
                                content.push("<button type='button' class='current'>" + i + "</button>");
                            }
                        }
                    }
                }
            } else {
                // 總頁數小於6
                for (var i = 1; i < totalNum + 1; i++) {
                    if (pageNum !== i) {
                        content.push("<button type='button'>" + i + "</button>");
                    } else {
                        content.push("<button type='button' class='current'>" + i + "</button>");
                    }
                }
            }
            content.push("<button type='button' id='lastPage'>尾頁</button><button type='button' id='nextPage'>下一頁</button>");
            content.push("<span class='totalNum'> 共 " + totalNum + " 頁 </span>");
            content.push("<span class='totalList'> 共 " + totalList + " 條記錄 </span>");
            me.element.html(content.join(''));

            // DOM重新生成後每次調用是否禁用button
            setTimeout(function () {
                me.dis();
            }, 20);
        },
        bindEvent: function () {
            var me = this;
            me.element.off('click', 'button');
            // 委託新生成的dom監聽事件
            me.element.on('click', 'button', function () {
                var id = $(this).attr('id');
                var num = parseInt($(this).html());
                var pageNum = me.options.pageNum;
                if (id === 'prePage') {
                    if (pageNum !== 1) {
                        me.options.pageNum -= 1;
                    }
                } else if (id === 'nextPage') {
                    if (pageNum !== me.options.totalNum) {
                        me.options.pageNum += 1;
                    }
                } else if (id === 'firstPage') {
                    if (pageNum !== 1) {
                        me.options.pageNum = 1;
                    }
                } else if (id === 'lastPage') {
                    if (pageNum !== me.options.totalNum) {
                        me.options.pageNum = me.options.totalNum;
                    }
                } else {
                    me.options.pageNum = num;
                }
                me.createHtml();
                if (me.options.callback) {
                    me.options.callback(me.options.pageNum);
                }
            });
        },
        dis: function () {
            var me = this;
            var pageNum = me.options.pageNum;
            var totalNum = me.options.totalNum;
            if (pageNum === 1) {
                me.element.children('#firstPage, #prePage').prop('disabled', true);
            } else if (pageNum === totalNum) {
                me.element.children('#lastPage, #nextPage').prop('disabled', true);
            }
        }
    };
    $.fn.paging = function (options) {
        return new Paging($(this), options);
    }
})(jQuery, window, document);

jQuery插件友情鏈接

// 1、代碼最前面的分號,可以防止多個文件壓縮合並以爲其他文件最後一行語句沒加分號,而引起合併後的語法錯誤。
// 2、匿名函數(function(){})();:由於Javascript執行表達式是從圓括號裏面到外面,所以可以用圓括號強制執行聲明的函數。避免函數體內和外部的變量衝突。
// 3、$實參:$是jquery的簡寫,很多方法和類庫也使用$,這裏$接受jQuery對象,也是爲了避免$變量衝突,保證插件可以正常運行。
// 4、window, document實參分別接受window, document對象,window, document對象都是全局環境下的,而在函數體內的window, document其實是局部變量,不是全局的window, document對象。這樣做有個好處就是可以提高性能,減少作用域鏈的查詢時間,如果你在函數體內需要多次調用window 或 document對象,這樣把window 或 document對象當作參數傳進去,這樣做是非常有必要的。當然如果你的插件用不到這兩個對象,那麼就不用傳遞這兩個參數了。
// 5、undefined形參了,看起來是有點多餘。undefined在老一輩的瀏覽器是不被支持的,直接使用會報錯,js框架要考慮到兼容性,因此增加一個形參undefined

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