jquery.pager.js /** * 分页插件 * Created by ck on 2017/4/17. */;(function ($, window, document, undefined) { if ($.fn.pager) { console.error('可能与其他插件冲突'); } var options = {}; var leftCount, rightCount, threshold; /** *
/** * 分页插件 * Created by ck on 2017/4/17. */ ;(function ($, window, document, undefined) { if ($.fn.pager) { console.error('可能与其他插件冲突'); } var options = {}; var leftCount, rightCount, threshold; /** * @param args * @constructor */ var Pager = function (args) { var defaults = { pageSize: 10, aroundCount: 1, leftCount: 1, rightCount: 1 }; this.pageRange = []; $.extend(options, defaults, args); var centerCount = options.aroundCount * 2 + 1; leftCount = options.leftCount || centerCount; rightCount = options.rightCount || centerCount; threshold = leftCount + centerCount + rightCount; // 阈值 }; Pager.prototype = { reset: function (args) { options = {}; $.extend(options, defaults, args); var centerCount = options.aroundCount * 2 + 1; leftCount = options.leftCount || centerCount; rightCount = options.rightCount || centerCount; threshold = leftCount + centerCount + rightCount; // 阈值 }, update: function (totalCount, currentPage) { this.pageRange = []; if (!totalCount || !totalCount) return this; var i = 1; var pageCount = parseInt(totalCount / options.pageSize); totalCount % options.pageSize > 0 && pageCount++; this.pageCount = pageCount; this.currentPage = Math.max(1, Math.min(currentPage, pageCount)); if (pageCount <= threshold) { for (i = 1; i <= pageCount; i++) { this.pageRange.push(i); } } else { var leftPage = currentPage - options.aroundCount; var rightPage = currentPage + options.aroundCount; if (leftPage - options.leftCount <= 1) { // 与左侧有交集 rightPage = Math.max(rightPage, options.leftCount); for (i = 1; i <= rightPage; i++) { this.pageRange.push(i); } this.pageRange.push(null); for (i = pageCount - options.rightCount + 1; i <= pageCount; i++) { this.pageRange.push(i); } } else if (pageCount - rightPage <= options.rightCount) { // 与右侧有交集 for (i = 1; i <= options.leftCount; i++) { this.pageRange.push(i); } this.pageRange.push(null); leftPage = Math.min(leftPage, pageCount - options.rightCount + 1); for (i = leftPage; i <= pageCount; i++) { this.pageRange.push(i); } } else { // 居中 for (i = 1; i <= options.leftCount; i++) { this.pageRange.push(i); } this.pageRange.push(null); for (i = leftPage; i <= rightPage; i++) { this.pageRange.push(i); } this.pageRange.push(null); for (i = pageCount - options.rightCount + 1; i <= pageCount; i++) { this.pageRange.push(i); } } } return this; } }; var pager = null; var $element, $pagerWrapper, $pagerList, $pagerJumper; var updateHtml = function (totalCount, currentPage) { if (totalCount == 0) { $pagerWrapper.hide(); return false; } else { $pagerWrapper.show(); } var pageRange = pager.update(totalCount, currentPage).pageRange; currentPage = pager.currentPage; var htmlArray = []; if (currentPage == 1) { // 如果当前页面是第一页 htmlArray.push('