DIV CSS 佈局教程網

打造輕量分頁外掛 GB-paging 的完整教學
編輯:關於JavaScript     

簡介

這是一個相容於 IE6+ 與現代瀏覽器的輕量分頁外掛,並支援同一頁面存在多個分頁。

使用方式

瀏覽器環境

 <link rel="stylesheet" href="css/GB-paging.css"> <script src="js/GB-paging.js"></script> 

一般分頁

HTML

 <div id="gbpaging" class="gb-paging"></div> 

JS

 gbPaging({ total: 101, paramName: 'p', curPage: getPage('p', '#'), size: 5, prevText: '<', nextText: '>', pageInfo: '<b>{{currentPage}}</b>/<b>{{totalPages}}</b>', eventType: 'click' }); 

同一頁面的另一個分頁

HTML

 <div id="paging1" class="gb-paging"></div> 

JS

 gbPaging({ eleId: 'paging1', total: 54, size: 10, prevText: false, nextText: false, paramName: 'p1', curPage: getPage('p1', '#'), pageInfo: false, eventType: 'click' }); 

帶參數重新整理

HTML

 <div id="paging2" class="gb-paging"></div> 

JS

 gbPaging({ eleId: 'paging2', total: 2500, paramName: 'page', curPage: getPage('page', '?type=1&name=2&'), goUrl: '?type=1&name=2&{{n}}' }); 

輔助函式

 function getPage(pname, other){ var page = window.location.search.replace( other + pname + '=', '') || 1; return parseInt(page); } 

選項說明

eleId:分頁容器,僅支援 ID,預設為 'gbpaging'。
total:總記錄數。
paramName:分頁參數名,預設 'p'。
curPage:當前頁碼。
size:每頁記錄數,預設 10。
prevText:上一頁文案,預設 '上一頁',設為 false 則不顯示。
nextText:下一頁文案,預設 '下一頁',設為 false 則不顯示。
pageInfo:分頁資訊,{{currentPage}} 為當前頁碼,{{totalPages}} 為總頁數,設為 false 則不顯示。
eventType:事件方式,'click' 或 'link'。
goUrl:連結 URL,例如 '?type=1&name=2&{{n}}',其中 {{n}} 為分頁參數名加值,如 'p=1'。

Github

https://github.com/givebest/GB-paging

完整程式碼

 /** * GB-paging.js * @class gbPaging * @see https://github.com/givebest/GB-paging * @author givenlovs@msn.com * @(c) 2016 **/ (function() { var $, defaultConfig, paging = null, args; /** * [defaultConfig 預設配置] * @type {Object} */ defaultConfig = { eventType: 'link', curPage: 1, //當前頁 size: 10, //每頁 n 條 paramName: 'p', //頁碼參數 prevText: '上一頁', nextText: '下一頁', pageInfo: '當前第<b>{{currentPage}}</b>頁/共<b>{{totalPages}}</b>頁' } /** * [$ description] * @param {String} id * @return {Object} HTML [HTML element] */ $ = function(id) { return document.getElementById(id); } function GbPaging() { if (!(this instanceof GbPaging)) return new GbPaging(); } GbPaging.prototype.init = function(opts) { this.getParams(opts); this.build(); this.events(); } GbPaging.prototype.getParams = function(opts) { args = this.opts = opts || {}; if ($(args.eleId)) { args.container = $(args.eleId); } else if ($('gbpaging')) { args.container = $('gbpaging'); } else { return; } args.eventType = args.eventType || defaultConfig.eventType; args.total = args.total || 0; args.curPage = args.curPage || defaultConfig.curPage; args.size = args.size || defaultConfig.size; args.paramName = args.paramName || defaultConfig.paramName; args.goUrl = args.goUrl || ''; args.pages = Math.ceil(args.total / args.size || 0); args.isPrev = (args.prevText !== false) ? true : false; args.prevText = args.prevText || defaultConfig.prevText; args.isNext = (args.nextText !== false) ? true : false; args.nextText = args.nextText || defaultConfig.nextText; args.isPageInfo = (args.pageInfo !== false) ? true : false; args.pageInfo = args.pageInfo || defaultConfig.pageInfo; } /** * [分頁 html] */ GbPaging.prototype.build = function() { var html = []; if (args.pages <= 1) { addClass(args.container, 'gb-hide'); return; } if (args.curPage >>> 0 <= 0) return; var ii = (args.curPage == 1 || args.curPage == args.pages) ? 3 : 2; var urlParam = parseUrl(args.goUrl, args.paramName); html.push('<a href="'+ urlParam + args.curPage +'" class="gb-paging-current">' + args.curPage + '</a>'); for (var i = 1; i < ii; i++) { if (args.curPage - i > 1) { html.unshift('<a href="'+ urlParam + (args.curPage - i) +'">' + (args.curPage - i) + '</a>'); } if (args.curPage + i < args.pages) { html.push('<a href="'+ urlParam + (args.curPage + i) +'">' + (args.curPage + i) + '</a>'); } } if (args.curPage - 2 > 1) { html.unshift('<span class="gb-paging-ellipsis">...</span>'); } if (args.curPage > 1) { html.unshift('<a href="'+ urlParam +'1">1</a>'); args.isPrev && html.unshift('<a href="'+ urlParam + (args.curPage - 1) +'">' + args.prevText + '</a>'); } else { args.isPrev && html.unshift('<a class="disabled">' + args.prevText + '</a>'); } if (args.curPage + 2 < args.pages) { html.push('<span class="gb-paging-ellipsis">...</span>'); } if (args.curPage < args.pages) { html.push('<a href="'+ urlParam + args.pages +'">' + args.pages + '</a>'); args.isNext && html.push('<a href="'+ urlParam + (args.curPage + 1) +'">' + args.nextText + '</a>'); } else { args.isNext && html.push('<a class="disabled">' + args.nextText + '</a>'); } if (args.isPageInfo) html.push('<div class="gb-paging-info">' + args.pageInfo.replace('{{currentPage}}', args.curPage).replace('{{totalPages}}', args.pages) + '</div>'); args.container.innerHTML = html.join(''); } /** * [綁定事件] */ GbPaging.prototype.events = function() { var _this = this; if (args.eventType !== 'click') return; bind(args.container, 'click', function(e) { e = e || window.event; e.preventDefault ? e.preventDefault() : e.returnValue = false; args = _this.opts; var target = e.target || e.srcElement; if (target.tagName.toLowerCase() === 'a' && !hasClass(target, 'disabled')) { args.curPage = target.getAttribute('href').replace(parseUrl(args.goUrl, args.paramName), ''); if (args.curPage >>> 0 === 0) args.curPage = args.curPage.replace(window.location.href, ''); args.curPage = parseInt(args.curPage); _this.build(); } }); } /** * [解析 URL] */ function parseUrl(url, param) { if (url) { return url.replace('{{n}}', param + '='); } else { return '#' + param + '='; } } /** * Bind events to elements */ function bind(ele, event, fn) { if (typeof addEventListener === 'function') { ele.addEventListener(event, fn, false); } else if (ele.attachEvent) { ele.attachEvent('on' + event, fn); } } /** * hasClass */ function hasClass(ele, cls) { if (!ele || !cls) return false; if (ele.classList) { return ele.classList.contains(cls); } else { return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); } } /** * addClass */ function addClass(ele, cls) { if (ele.classList) { ele.classList.add(cls); } else { if (!hasClass(ele, cls)) ele.className += '' + cls; } } if (window.gbPaging === undefined) { window.gbPaging = function (opts) { paging = GbPaging(); return paging.init(opts); } } // AMD if (typeof define == 'function' && define.amd) { define('GB-paging', [], function() { return gbPaging; }); } }()); 

以上便是這個輕量分頁外掛的完整說明與程式碼,你可以依需求調整選項,快速在自己的專案中導入分頁功能。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved