分享我对JS插件开发的一些感想和心得(2)

<div> <span>上一页</span> <span>1</span> <a page="1" href="javascript:;">2</a> <a page="2" href="javascript:;">3</a> <a page="3" href="javascript:;">4</a> <span>...</span> <a href="javascript:;" page="8">9</a> <a page="1" href="javascript:;">下一页</a> </div>

这是最基本html代码结构,包含了分页插件的容器div.pager,当前页span.curPage,其他页码a标签,上一页,下一页等按钮。

接着是css代码,主要当前页标签,其他页面标签,上一页下一页,鼠标悬停在按钮上等几种样式,编写如下:

.pager { display: inline-block; font: 12 px/21px "宋体"; margin-top: 20px; } .pager a, .pager .flip, .pager .curPage { border: 1px solid #e3e3e3; display: inline-block; height: 22px; line-height: 22px; text-align: center; } .pager a { background: none repeat scroll 0 0 #fff; color: #010101; text-decoration: none; width: 26px; } .pager a:hover { background: none repeat scroll 0 0 #f1f1f1; } .pager .noPage { color: #a4a4a4; } .pager .curPage { background: none repeat scroll 0 0 #49abde; color: #ffffff; width: 26px; } .pager .flip { width: 56px; }

编写js代码

写好基本的html和css,接下来最关键的就是js代码了。首先我们搭建好jQuery插件开发的基本形式:

; (function ($, window, document, undefined) { "use strict"; var defaults = { pageIndex: 0, pageSize: 6, itemCount: 50, maxButtonCount: 7, prevText: "上一页", nextText: "下一页", buildPageUrl: null, onPageChanged: null }; $.fn.pager = function (options) { options = $.extend(defaults, options || {}); } })(jQuery, window, document);

这里主要提供了一些可选参数的默认值,比如页码默认为0,每页数量6条等等。

接着我们来考虑一下分页插件的思路:

1、设置当前页码为0,表示第一页
2、生成分页插件的html代码
3、修改页码,再生成html代码

基于这个思路,我们编写代码如下:

; (function ($, window, document, undefined) { "use strict"; var defaults = { pageIndex: 0, pageSize: 6, itemCount: 50, maxButtonCount: 7, prevText: "上一页", nextText: "下一页", buildPageUrl: null, onPageChanged: null }; function Pager($ele, options) { this.$ele = $ele; this.options = options = $.extend(defaults, options || {}); this.init(); } Pager.prototype = { constructor: Pager, init: function () { this.renderHtml(); this.bindEvent(); }, renderHtml: function () { var options = this.options; options.pageCount = Math.ceil(options.itemCount / options.pageSize); var html = []; //生成上一页的按钮 if (options.pageIndex > 0) { html.push('<a page="' + (options.pageIndex - 1) + '" href="' + this.buildPageUrl(options.pageIndex + 1) + '">' + options.prevText + '</a>'); } else { html.push('<span>' + options.prevText + '</span>'); } //这里是关键 //临时的起始页码中间页码,当页码数量大于显示的最大按钮数时使用 var tempStartIndex = options.pageIndex - Math.floor(options.maxButtonCount / 2) + 1; //计算终止页码,通过max计算一排按钮中的第一个按钮的页码,然后计算出页数量 var endIndex = Math.min(options.pageCount, Math.max(0, tempStartIndex) + options.maxButtonCount) - 1; var startIndex = Math.max(0, endIndex - options.maxButtonCount + 1); // 第一页 if (startIndex > 0) { html.push("<a href='" + this.buildPageUrl(0) + "' page='" + 0 + "'>1</a> "); html.push("<span>...</span>"); } //生成页码按钮 for (var i = startIndex; i <= endIndex; i++) { if (options.pageIndex == i) { html.push('<span>' + (i + 1) + '</span>'); } else { html.push('<a page="' + i + '" href="' + this.buildPageUrl(options.pageIndex + 1) + '">' + (i + 1) + '</a>'); } } // 最后一页 if (endIndex < options.pageCount - 1) { html.push("<span>...</span> "); html.push("<a href='" + this.buildPageUrl(options.pageCount - 1) + "' page='" + (options.pageCount - 1) + "'>" + options.pageCount + "</a> "); } //生成下一页的按钮 if (options.pageIndex < options.pageCount - 1) { html.push('<a page="' + (options.pageIndex + 1) + '" href="' + this.buildPageUrl(options.pageIndex + 1) + '">' + options.nextText + '</a>'); } else { html.push('<span>' + options.nextText + '</span>'); } this.$ele.html(html.join("")); }, bindEvent: function () { var that = this; that.$ele.on("click", "a", function () { that.options.pageIndex = parseInt($(this).attr("page"), 10); that.renderHtml(); that.options.onPageChanged && that.options.onPageChange(that.options.pageIndex); }) }, buildPageUrl: function () { if ($.isFunction(this.options.buildPageUrl)) { return this.options.buildPageUrl(pageIndex); } return "javascript:;"; } }; $.fn.pager = function (options) { options = $.extend(defaults, options || {}); return new Pager($(this), options); } })(jQuery, window, document);

这段代码中有两个关键点要记住:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwwzd.html