做過前端開發的人大概都有過這樣的糾結:頁面需要某個效果時,到底是上網找插件,還是自己寫一個?這兩條路各有代價,下面是我的判斷標準與一套輕量化插件的開發方法。
如果能找到剛好符合需求的插件,那當然幸運。但實踐中經常遇到幾類問題:
當然,並非不能用現成插件。以下幾種情況我會毫不猶豫地選現成方案:功能確實複雜(如文件批量上傳、富文本編輯器);工期緊張且對性能不敏感;插件剛好命中需求。
自己寫插件的成本也很明確:需要投入時間,可能拖延工期;自製輪子未必比成熟方案完善,還要考慮同事的上手成本;對開發水平也有要求。
但如果項目並不緊急,我傾向於自己造,理由有幾點:
所謂適應性強,我理解就是兩條:對 UI 的限制越少越好,最好沒有;功能不要貪多,只提供簡單 API,把擴展空間留給使用者。
以 jQuery 分頁插件為例說明流程。
分頁插件最核心的需求只有兩個:顯示頁碼、切換頁碼。先圍繞這兩點做,暫時不要糾結跳轉輸入框、每頁條數可調等潛在需求。
結構越簡單越好,a 與 span 混排即可,不必引入 ul/li 增加層級:
<div class="pager"> <span class="flip noPage">上一頁</span> <span class="curPage">1</span> <a page="1" href="javascript:;">2</a> <a page="2" href="javascript:;">3</a> <span>...</span> <a href="javascript:;" page="8">9</a> <a href="javascript:;" page="1" class="flip">下一頁</a> </div>
.pager { display: inline-block; font: 12px/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: #fff; color: #010101; text-decoration: none; width: 26px; }
.pager a:hover { background: #f1f1f1; }
.pager .noPage { color: #a4a4a4; }
.pager .curPage { background: #49abde; color: #fff; width: 26px; }
.pager .flip { width: 56px; }
先搭好 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 表示第一頁;根據頁碼生成 HTML;頁碼變化時重新生成 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) + '" class="flip">' + options.prevText + '</a>');
} else {
html.push('<span class="flip noPage">' + options.prevText + '</span>');
}
// 計算當前應顯示的頁碼區間
var tempStartIndex = options.pageIndex - Math.floor(options.maxButtonCount / 2) + 1;
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 class="curPage">' + (i + 1) + '</span>');
} else {
html.push('<a page="' + i + '" href="' + this.buildPageUrl(i) + '">' + (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) + '" class="flip">' + options.nextText + '</a>');
} else {
html.push('<span class="flip noPage">' + 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.onPageChanged(that.options.pageIndex);
});
},
buildPageUrl: function (pageIndex) {
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);
兩個關鍵點值得記住:一是 HTML 生成,頁碼過多時需要用省略號遮罩,由 maxButtonCount 控制最多顯示幾個按鈕;二是事件綁定,每次頁碼變化都會重繪 HTML,採用事件代理既提高性能,也免去反覆綁定。
如果之後要支持「輸入頁碼直接跳轉」,是否得改 HTML 結構和 CSS?我的方案是:提供簡單 API,不提供 UI,UI 完全由使用者自定義。
getPageIndex: function () {
return this.options.pageIndex;
},
setPageIndex: function (pageIndex) {
this.options.pageIndex = pageIndex;
this.renderHtml();
},
setItemCount: function (itemCount) {
this.options.pageIndex = 0;
this.options.itemCount = itemCount;
this.renderHtml();
}
有了這三個方法,需要跳轉頁碼的使用者自己調 setPageIndex 並畫一個輸入框即可,插件本身只專注基本功能,不干預其他。
這是我在寫 JS 插件時權衡「輕量化」與「適應性」的一些想法,歡迎交流探討。