剛寫的一個js分頁類,未最佳化

來源:互聯網
上載者:User

剛寫的一個js分頁類,未最佳化

以前也寫過,很久以前了,寫的很長,並且使用起來也不靈活。這次搞了個靈活版的。

/* * SunPage --自訂分頁類,僅擷取分頁的html代碼,簡單應用 * Sunbt 2015-5-4 * v1.0 * @param current_page int 當前頁 * @param totle_page int 總頁數 * @param take_num int 當前頁左右攜帶頁碼數 * @param func_obj obj 分頁實現函數*/var SunPage = function(current_page, totle_page, take_num, func_obj) {    this.current_page = current_page;    this.totle_page = totle_page;    this.take_num = take_num;    this.func_obj = func_obj;    this.page_html = "";    this.init();}SunPage.prototype = {    constructor : SunPage,    init : function() {        var start = this.current_page - this.take_num < 2 ? 2 : this.current_page - this.take_num, //起始索引            _end_patch = start - (this.current_page - this.take_num), //右側補碼            _end = this.current_page + this.take_num + _end_patch, //計算結束索引            end = _end < this.totle_page ? _end : this.totle_page - 1, //實際結束索引            _start_patch = _end -  end; //左側補碼        if (_start_patch > 0 && _end_patch == 0) { //如果有左側補碼,並且右側補碼為0,進行左側側補碼            start -= _start_patch;            start = start < 2 ? 2 : start;        }        this.func_obj.previous.call(this); //上一頁        this.func_obj.common.call(this, 1); //第一頁        if (start == 3) {            this.func_obj.common.call(this, 2); //開始索引為三,第二頁直接顯示        } else if(start > 3) {            this.func_obj.ellipsis.call(this); //開始索引大於三,顯示省略符號        }        for (var i = start; i <= end; i++) {            this.func_obj.common.call(this, i); //從索引開始到結束        }        if (end < this.totle_page - 2) {            this.func_obj.ellipsis.call(this); //結束索引為總頁碼減二,則顯示總頁碼減一的頁碼        } else if(end == this.totle_page - 2) {            this.func_obj.common.call(this, this.totle_page - 1); //        }        this.func_obj.common.call(this, this.totle_page);        this.func_obj.next.call(this);    },    toString :function() {        return this.page_html;    }}

嘿嘿,靈活也要付出代價的,就是調用的時候會複雜些。下面是調用方式:

var sunpage = new SunPage(6, 20, 2, {    previous : function() {        if(this.current_page == 1) {            this.page_html += '
  • 上一頁
  • '; } else { this.page_html += '
    • 上一頁
    • '; } }, next : function() { if(this.current_page == this.totle_page) { this.page_html += '
      • 下一頁
      • '; } else { this.page_html += '
        • 下一頁
        • '; } }, ellipsis : function() { this.page_html += '
          • ...
          • '; }, common :function(num){ if(num == this.current_page) { this.page_html += '
            • ' + num + '
            • '; } else { this.page_html += '
              • ' + num + '
              • '; } } }); document.write(sunpage.toString());

                很多地方都沒做校正的,有問題後再修改吧。目前測試沒問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.