jQuery分頁插件

HTML:javascript

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8"/>
 5     <title></title>
 6     <style>
 7         /* 外面盒子樣式---本身定義 */
 8  .page_div{margin:20px 10px 20px 0;color:#666}
 9         /* 頁數按鈕樣式 */
10  .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;}
11  #firstPage,#lastPage,#nextPage,#prePage{width:50px;color:#0073A9;border:1px solid #0073A9}
12  #nextPage,#prePage{width:70px}
13  .page_div .current{background-color:#0073A9;border-color:#0073A9;color:#FFF}
14         /* 頁面數量 */
15  .totalPages{margin:0 10px}
16  .totalPages span,.totalSize span{color:#0073A9;margin:0 5px}
17         /*button禁用*/
18  .page_div button:disabled{opacity:.5;cursor:no-drop}
19     </style>
20 </head>
21 <body>
22     <div id="page" class="page_div"></div>
23 </body>
24 <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
25 <script type="text/javascript" src="pageMe.js"></script>
26 <script>
27     // pageMe.js 使用方法
28  $("#page").paging({ 29  pageNum: 5, // 當前頁面
30  totalNum: 14, // 總頁碼
31  totalList: 300, // 記錄總數量
32  callback: function (num) { //回調函數
33  console.log(num); 34  } 35  }); 36 </script>
37 </html>

pageMe.js  html

 1 ;(function ($, window, document, undefined) {  2  'use strict';  3  function Paging(element, options) {  4  this.element = element;  5  this.options = {  6  pageNum: options.pageNum || 1, // 當前頁碼  7  totalNum: options.totalNum, // 總頁碼  8  totalList: options.totalList, // 數據總記錄  9  callback: options.callback // 回調函數  10  };  11  this.init();  12  }  13  Paging.prototype = {  14  constructor: Paging,  15  init: function () {  16  this.createHtml();  17  this.bindEvent();  18  },  19  createHtml: function () {  20  var me = this;  21  var content = [];  22  var pageNum = me.options.pageNum;  23  var totalNum = me.options.totalNum;  24  var totalList = me.options.totalList;  25             content.push("<button type='button' id='firstPage'>首頁</button><button type='button' id='prePage'>上一頁</button>");  26  // 總頁數大於6必顯示省略號  27  if (totalNum > 6) {  28  // 一、當前頁碼小於5且總頁碼大於6 省略號顯示後面+總頁碼  29                 if (pageNum < 5) {  30  // 1與6主要看要顯示多少個按鈕 目前都顯示5個  31  for (var i = 1; i < 6; i++) {  32  if (pageNum !== i) {  33  content.push("<button type='button'>" + i + "</button>");  34  } else {  35                             content.push("<button type='button' class='current'>" + i + "</button>");  36  }  37  }  38  content.push(". . .");  39                     content.push("<button type='button'>" + totalNum + "</button>");  40  } else {  41  // 二、當前頁碼接近後面 到最後頁碼隔3個 省略號顯示後面+總頁面  42                     if (pageNum < totalNum - 3) {  43  for (var i = pageNum - 2; i < pageNum + 3; i++) {  44  if (pageNum !== i) {  45  content.push("<button type='button'>" + i + "</button>");  46  } else {  47                                 content.push("<button type='button' class='current'>" + i + "</button>");  48  }  49  }  50  content.push(". . .");  51                         content.push("<button type='button'>" + totalNum + "</button>");  52  } else {  53  // 三、頁碼至少在5,最多在【totalNum - 3】的中間位置 第一頁+省略號顯示前面  54                         content.push("<button type='button'>" + 1 + "</button>");  55  content.push(". . .");  56                         for (var i = totalNum - 4; i < totalNum + 1; i++) {  57  if (pageNum !== i) {  58  content.push("<button type='button'>" + i + "</button>");  59  } else {  60                                 content.push("<button type='button' class='current'>" + i + "</button>");  61  }  62  }  63  }  64  }  65  } else {  66  // 總頁數小於6  67                 for (var i = 1; i < totalNum + 1; i++) {  68  if (pageNum !== i) {  69  content.push("<button type='button'>" + i + "</button>");  70  } else {  71                         content.push("<button type='button' class='current'>" + i + "</button>");  72  }  73  }  74  }  75             content.push("<button type='button' id='lastPage'>尾頁</button><button type='button' id='nextPage'>下一頁</button>");  76             content.push("<span class='totalNum'> 共 " + totalNum + " 頁 </span>");  77             content.push("<span class='totalList'> 共 " + totalList + " 條記錄 </span>");  78  me.element.html(content.join(''));  79 
 80  // DOM從新生成後每次調用是否禁用button  81  setTimeout(function () {  82  me.dis();  83  }, 20);  84  },  85  bindEvent: function () {  86  var me = this;  87  me.element.off('click', 'button');  88  // 委託新生成的dom監聽事件  89  me.element.on('click', 'button', function () {  90  var id = $(this).attr('id');  91  var num = parseInt($(this).html());  92  var pageNum = me.options.pageNum;  93  if (id === 'prePage') {  94  if (pageNum !== 1) {  95  me.options.pageNum -= 1;  96  }  97  } else if (id === 'nextPage') {  98  if (pageNum !== me.options.totalNum) {  99  me.options.pageNum += 1; 100  } 101  } else if (id === 'firstPage') { 102  if (pageNum !== 1) { 103  me.options.pageNum = 1; 104  } 105  } else if (id === 'lastPage') { 106  if (pageNum !== me.options.totalNum) { 107  me.options.pageNum = me.options.totalNum; 108  } 109  } else { 110  me.options.pageNum = num; 111  } 112  me.createHtml(); 113  if (me.options.callback) { 114  me.options.callback(me.options.pageNum); 115  } 116  }); 117  }, 118  dis: function () { 119  var me = this; 120  var pageNum = me.options.pageNum; 121  var totalNum = me.options.totalNum; 122  if (pageNum === 1) { 123  me.element.children('#firstPage, #prePage').prop('disabled', true); 124  } else if (pageNum === totalNum) { 125  me.element.children('#lastPage, #nextPage').prop('disabled', true); 126  } 127  } 128  }; 129  $.fn.paging = function (options) { 130  return new Paging($(this), options); 131  } 132 })(jQuery, window, document);
相關文章
相關標籤/搜索