本文把 jQuery UI 進度條(progressbar)元件的核心觀念整理成一份容易查閱的中文說明,涵蓋適用時機、選項、事件、方法與主題客製。
進度條用來呈現「目前完成了百分之多少」。它的寬度預設會撐滿父容器,也可以透過 CSS 自由調整。要特別注意的是,這是一個精確型的進度條——只有當你能確實算出完成百分比時才適用;如果無法得知精確進度,改用不確定型進度條(模糊進度條)或 spinner 動畫,使用者體驗會更好。
jquery:jQuery 核心函式庫jquery.ui.core:jQuery UI 核心函式庫value 是進度條唯一的選項,型別為數字,預設值 0,用來設定初始化時的完成百分比。
// 初始化
$('.selector').progressbar({ value: 37 });
// 取得屬性值
var value = $('.selector').progressbar('option', 'value');
// 設定屬性值
$('.selector').progressbar('option', 'value', 37);
當進度條的 value 改變時觸發,事件型別名稱為 progressbarchange。回呼函式會收到兩個參數:event(原生瀏覽器事件)與 ui(jQuery UI 物件);函式中的 this 等同於 event.target。
// 初始化時綁定
$("#progressbar").progressbar({
change: function (event, ui) {
alert('又完成了 1%');
}
});
// 動態綁定:事件名即事件型別
$("#progressbar").bind('progressbarchange', function (event, ui) {
alert('又完成了 1%');
});
destroy:徹底移除進度條,讓元素回到被初始化之前的狀態。$("#progressbar").progressbar('destroy');disable:把進度條切換為失效狀態,與 enable 相對。$(".selector").progressbar('disable');enable:把進度條切換為可用狀態。$(".selector").progressbar('enable');option:取得或設定選項。只給選項名為「取得」,同時給值為「設定」。$(".selector").progressbar('option', optionName[, value]);value:取得或設定目前的完成度。$(".selector").progressbar('value'[, value]);一般性的配色調整可以直接用 jQuery UI 的線上主題範製工具完成。若要完全自訂,則修改 ui.progressbar.css,其中兩個關鍵 class 分別是:
ui-progressbar:影響進度條的外框容器樣式。ui-progressbar-value:影響進度條本身的樣式。把下面的程式碼存成一個 HTML 檔直接開啟,就能操作 init、destroy、disable、enable、option、value 等方法,並看到動態遞增的動畫效果。
<!doctype html>
<html lang="zh-Hant">
<head>
<title>jQuery UI Progressbar - 基本功能</title>
<link type="text/css" href="../../themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../ui/ui.core.js"></script>
<script type="text/javascript" src="../../ui/ui.progressbar.js"></script>
<script type="text/javascript">
$(function () {
var current_value = 0;
change = function () {
$("#progressbar").progressbar('option', 'value', current_value);
current_value++;
if (current_value <= 100) setTimeout('change()', 200);
};
progressbar_init = function () {
$("#progressbar").progressbar({
value: current_value,
change: function (event, ui) {
// 讓數字標籤跟著進度移動
$('#number').text($(this).progressbar('option', 'value'));
document.getElementById('number').style.position = 'relative';
document.getElementById('number').style.left =
$(this).progressbar('option', 'value') * ($('.ui-progressbar').width() / 100) - 5;
}
});
};
progressbar_destroy = function () { $("#progressbar").progressbar('destroy'); };
progressbar_disable = function () { $("#progressbar").progressbar('disable'); };
progressbar_enable = function () { $("#progressbar").progressbar('enable'); };
progressbar_dynamic = function () { progressbar_init(); change(); };
progressbar_option = function () {
var option_name = $('#option_name').val();
var option_value = $('#option_value').val();
if (option_value) $("#progressbar").progressbar('option', option_name, option_value);
else $('#option_value').val($("#progressbar").progressbar('option', option_name));
};
progressbar_value = function () {
var value_value = $('#value_value').val();
if (value_value) $("#progressbar").progressbar('value', value_value);
else $('#value_value').val($("#progressbar").progressbar('option', 'value'));
};
});
</script>
</head>
<body>
<div id="number"></div>
<div id="progressbar"></div>
<input type="button" value="init" onclick="progressbar_init();" />
<input type="button" value="destroy" onclick="progressbar_destroy();" />
<input type="button" value="disable" onclick="progressbar_disable();" />
<input type="button" value="enable" onclick="progressbar_enable();" />
<input type="button" value="dynamic" onclick="progressbar_dynamic();" />
<input type="button" value="option" onclick="progressbar_option();" />
選項名: <input type="text" id="option_name" />
選項值: <input type="text" id="option_value" />
<input type="button" value="value" onclick="progressbar_value();" />
完成度: <input type="text" id="value_value" />
</body>
</html>
progressbar 的核心其實只有兩個關鍵字:value 選項與 value 方法。只要用自己的程式去驅動 value,進度條就會跟著呈現目前的完成狀態;其餘的 destroy、disable、enable 都是圍繞這個主軸的輔助操作。