xmlplus組件設(shè)計(jì)系列之下拉刷新(PullRefresh)(6)
“下拉刷新”由著名設(shè)計(jì)師 Loren Brichter 設(shè)計(jì),并應(yīng)用于 Twitter 第三方應(yīng)用 Tweetie 中。2010年4月,Twitter 收購 Tweetie 開發(fā)商 Atebits 后,該專利歸 Twitter 所有。這一章我們就來看看如何實(shí)現(xiàn)一個(gè)簡單的下拉刷新組件。

目標(biāo)組件分析
和前面在設(shè)計(jì)組件時(shí)的做法一樣,我們先想想看最終的成品組件是如何使用的,這需要點(diǎn)想像力。下拉刷新組件看成一個(gè)容器組件是合理的,用戶可以對容器的內(nèi)容進(jìn)行下拉操作。如果用戶完成了完整的下拉觸發(fā)操作,該組件應(yīng)該會(huì)有下拉完成的事件反饋,假定這個(gè)事件名為 ready。根據(jù)以上的分析,我們很有可能得到下面的一個(gè)該組件的應(yīng)用示例。
Example1: {
xml: `<PullRefresh id='example'>
<h1>Twitter</h1>
<h2>Loren Brichter</h2>
</PullRefresh>`,
fun: function (sys, items, opts) {
sys.example.on("ready", () => console.log("ready"));
}
}
示例中的使用方式是非常簡潔的,但我們還漏了一點(diǎn)。如果你用過一些新聞客戶端,在某些情況下,此客戶端會(huì)自動(dòng)觸發(fā)下拉刷新操作。比如,剛進(jìn)入客戶端頁面或者由于軟件推送機(jī)制產(chǎn)生的被動(dòng)列表更新,這都將導(dǎo)致客戶端下拉刷新操作的觸發(fā)。所以如上的 PullRefresh 組件還應(yīng)該提供一個(gè)觸發(fā)自動(dòng)刷新的操作接口。好了,下面是加入下拉刷新接口的應(yīng)用示例。
Example2: {
xml: `<PullRefresh id='example'>
<h1>Twitter</h1>
<h2>Loren Brichter</h2>
<button id='refresh'>click</button>
</PullRefresh>`,
fun: function (sys, items, opts) {
sys.example.on("ready", () => console.log("ready"));
sys.refresh.on("click", items.example.refresh);
}
}
基本框架
現(xiàn)在讓我們把目光轉(zhuǎn)移到下拉刷新組件的內(nèi)部,看看該如何去實(shí)現(xiàn)。觀察文章開始部分的大圖,很自然地我們可以將整個(gè)組件劃分為三個(gè)子組件,如下面的 XML 文檔所示。
<div id="refresh"> <Status id="status"/> <div id="content"></div> </div>
外圍 div 元素包含兩個(gè)子組件:其中一個(gè)是狀態(tài)指示條,用于顯示“下拉刷新”、“松開刷新”、“加載中...”以及“刷新成功”四個(gè)狀態(tài)提示,這里暫時(shí)使用未定義的 Status 組件替代;另一個(gè) div 元素用于容納下拉刷新組件的包含內(nèi)容。到現(xiàn)在,大概可以想得出該組件的工作邏輯了,于是我們可以給出下面的一個(gè)基本的組件框架。
PullRefresh: {
css: "#refresh { position: relative; height: 100%;...}",
xml: `<div id="refresh">
<Status id="status"/>
<div id="content"/>
</div>`,
map: { appendTo: "content" },
fun: function (sys, items, opts) {
sys.content.on("touchstart", e => {
// 偵聽 touchmove 和 touchend事件
});
function touchmove(e) {
// 1 處理狀態(tài)條與內(nèi)容內(nèi)面跟隨觸點(diǎn)移動(dòng)
// 2 根據(jù)觸點(diǎn)移動(dòng)的距離顯示相當(dāng)?shù)臓顟B(tài)條內(nèi)容
}
function touchend(e) {
// 1 移除 touchmove 和 touchend 事件
// 2 根據(jù)觸點(diǎn)移動(dòng)的距離決定返回原始狀態(tài)或者進(jìn)入刷新狀態(tài)并派發(fā)事件
}
}
}
狀態(tài)條的實(shí)現(xiàn)
如前面提到的,狀態(tài)條組件包含四個(gè)狀態(tài)提示,并且每一時(shí)刻僅顯示一個(gè)狀態(tài)。對于狀態(tài)的切換,這里會(huì)先用到我們下一章將講到的路由組件 ViewStack,這里僅需要了解如何使用即可。組件 ViewStack 對外只顯示子級(jí)的一個(gè)子組件,同時(shí)偵聽一個(gè) switch 事件,該事件的派發(fā)者攜帶了一個(gè)切換到的目標(biāo)對象的名稱,也就是 ID。該組件根據(jù)這個(gè) ID 來切換到目標(biāo)視圖。下面是狀態(tài)條組件的完整實(shí)現(xiàn)。
Status: {
css: "#statusbar { height: 2.5em; line-height: 2.5em; text-align: center; }",
xml: <ViewStack id="statusbar">
<span id="pull">下拉刷新</span>
<span id="ready">松開刷新</span>
<span id="loading">加載中...</span>
<span id="success">刷新成功</span>
</ViewStack>,
fun: function (sys, items, opts) {
var stat = "pull";
function getValue() {
return stat;
}
function setValue(value) {
sys.statusbar.trigger("switch", stat = value);
}
return Object.defineProperty({}, "value", { get: getValue, set: setValue });
}
}
該組件提供一個(gè) value 接口用戶設(shè)置與獲取組件的顯示狀態(tài)。父級(jí)組件可根據(jù)不同的時(shí)機(jī)調(diào)用該接口。
最終實(shí)現(xiàn)
有了上面的儲(chǔ)備,讓我們來填充完下拉刷新組件的細(xì)節(jié)。下拉刷新過程中會(huì)涉及到動(dòng)畫,對于動(dòng)畫目前一般有兩種選擇,可以使用 JQuery 動(dòng)畫函數(shù),也可以是 css3,這需要看各人喜好了。這里我們選擇使用 css3 來實(shí)現(xiàn)。為清晰起見,下面的實(shí)現(xiàn)僅給出函數(shù)部分,其余部分同上。
PullRefresh: {
fun: function (sys, items, opts) {
var startY, height = sys.status.height();
sys.content.on("stouchstart", e => {
if (items.status.value == "pull") {
startY = e.y;
sys.content.on("touchmove", touchmove).on("touchend", touchend);
sys.content.css("transition", "").prev().css("transition", "");
}
});
function touchmove(e) {
var offset = e.y - startY;
if ( offset > 0 ) {
sys.content.css("top", offset + "px");
sys.status.css("top", (offset - height) + "px");
items.status(offset > height ? "ready" : "pull");
}
}
function touchend (e) {
var offset = e.y - startY;
sys.content.off("touchmove").off("touchend");
sys.content.css("transition", "all 0.3s ease-in 0s").prev().css("transition", "all 0.3s ease-in 0s");
if ( offset < height ) {
sys.content.css("top", "0").prev().css("top", -height + "px");
} else {
items.status.value = "release";
sys.refresh.once("complete", complete);
sys.content.css("top", height + "px").prev().css("top", "0").trigger("ready");
}
}
function complete() {
items.status.value = "message";
setTimeout(() => {
sys.content.css("top", "0").prev().css("top", -height + "px");
sys.content.once("webkitTransitionEnd", e => items.status.value = "pull");
}, 300);
}
}
}
對于稍微有點(diǎn)復(fù)雜的組件,需要注意組件的組織歸類,盡量把具有相近功能的組件放在一起。為了便于敘述,上述所列出的組件示意總把它們視作是同一目錄,這一點(diǎn)讀者應(yīng)該能看出來。
本系列文章基于 xmlplus 框架。如果你對 xmlplus 沒有多少了解,可以訪問 www.xmlplus.cn。這里有詳盡的入門文檔可供參考。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- xmlplus組件設(shè)計(jì)系列之網(wǎng)格(DataGrid)(10)
- xmlplus組件設(shè)計(jì)系列之文本框(TextBox)(3)
- xmlplus組件設(shè)計(jì)系列之選項(xiàng)卡(Tabbar)(5)
- xmlplus組件設(shè)計(jì)系列之路由(ViewStack)(7)
- xmlplus組件設(shè)計(jì)系列之分隔框(DividedBox)(8)
- xmlplus組件設(shè)計(jì)系列之樹(Tree)(9)
- xmlplus組件設(shè)計(jì)系列之按鈕(2)
- xmlplus組件設(shè)計(jì)系列之列表(4)
- xmlplus組件設(shè)計(jì)系列之圖標(biāo)(ICON)(1)
相關(guān)文章
多種js圖片預(yù)加載實(shí)現(xiàn)方式分享
這篇文章主要為大家詳細(xì)介紹了多種js圖片預(yù)加載實(shí)現(xiàn)方式,包括html標(biāo)簽或css加載圖片、純js實(shí)現(xiàn)預(yù)加載,感興趣的小伙伴們可以參考一下2016-02-02
javascript showModalDialog 內(nèi)跳轉(zhuǎn)頁面的問題
在頁面中使用了showModalDialog,但是在跳轉(zhuǎn)鏈接時(shí),不會(huì)在當(dāng)前頁執(zhí)行,而是彈出一個(gè)新的頁面。2010-11-11

