/** * 功能:页码切换 * 作者:wjb * 时间:2019-11-25 */ function pagination($content, $wrap, options) { this.$wrap = $wrap; this.$content = $content; this.options = $.extend({}, pagination.defaultoptions, options); this.init(); } pagination.defaultoptions = { size: 4 }; pagination.prototype.init = function () { console.log(this.$content); var totalitemnum = this.$content.children().length; var totalpagenum = this.totalpagenum = math.ceil(totalitemnum / this.options.size); this.currentpage = 1; this.$wrap.empty(); this.$content.children(':gt(' + (this.options.size - 1) + ')').hide(); this.$wrap.append([ '', '', '', '', '', '' ].join('')); for (var i = 0; i < totalpagenum; i++) { var $btn = $('' + (i + 1) + ''); $btn.data('page', i + 1); this.$wrap.find('.num').append($btn); } this.$wrap.find('.num').children().eq(0).addclass('current'); this.initevents(); }; pagination.prototype.initevents = function () { var _this = this; var $prev = this.$wrap.find('.prev'); var $next = this.$wrap.find('.next'); var $num = this.$wrap.find('.num'); $prev.on('click', function () { _this.prev(); }); $next.on('click', function () { _this.next(); }); $num.on('click', '.page-item', function () { var page = $(this).data('page'); _this.goto(page); }); }; pagination.prototype.prev = function () { this.goto(this.currentpage - 1); }; pagination.prototype.next = function () { this.goto(this.currentpage + 1); }; pagination.prototype.goto = function (num) { if (typeof num !== 'number') { throw new error('e'); } if(num > this.totalpagenum || num <= 0) { return false; } this.currentpage = num; this.$wrap.find('.num') .children().eq(this.currentpage - 1) .addclass('current').siblings('.current') .removeclass('current'); var left = (this.currentpage - 1) * this.options.size; var right = left + this.options.size; var $shouldshow = this.$content.children().filter(function (index) { return left <= index && index < right; }); this.$content.children().hide(); $shouldshow.show(); };