欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

xmlplus組件設(shè)計(jì)系列之下拉刷新(PullRefresh)(6)

 更新時(shí)間:2017年05月03日 09:06:55   作者:qudou  
xmlplus 是一個(gè)JavaScript框架,用于快速開(kāi)發(fā)前后端項(xiàng)目。這篇文章主要介紹了xmlplus組件設(shè)計(jì)系列之下拉刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

目標(biāo)組件分析

和前面在設(shè)計(jì)組件時(shí)的做法一樣,我們先想想看最終的成品組件是如何使用的,這需要點(diǎn)想像力。下拉刷新組件看成一個(gè)容器組件是合理的,用戶(hù)可以對(duì)容器的內(nèi)容進(jìn)行下拉操作。如果用戶(hù)完成了完整的下拉觸發(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"));
 }
}

示例中的使用方式是非常簡(jiǎn)潔的,但我們還漏了一點(diǎn)。如果你用過(guò)一些新聞客戶(hù)端,在某些情況下,此客戶(hù)端會(huì)自動(dòng)觸發(fā)下拉刷新操作。比如,剛進(jìn)入客戶(hù)端頁(yè)面或者由于軟件推送機(jī)制產(chǎn)生的被動(dòng)列表更新,這都將導(dǎo)致客戶(hù)端下拉刷新操作的觸發(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)。觀察文章開(kāi)始部分的大圖,很自然地我們可以將整個(gè)組件劃分為三個(gè)子組件,如下面的 XML 文檔所示。

<div id="refresh">
 <Status id="status"/>
 <div id="content"></div>
</div>

外圍 div 元素包含兩個(gè)子組件:其中一個(gè)是狀態(tài)指示條,用于顯示“下拉刷新”、“松開(kā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 => {
   // 偵聽(tīng) 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īng)顟B(tài)并派發(fā)事件
  }
 }
}

狀態(tài)條的實(shí)現(xiàn)

如前面提到的,狀態(tài)條組件包含四個(gè)狀態(tài)提示,并且每一時(shí)刻僅顯示一個(gè)狀態(tài)。對(duì)于狀態(tài)的切換,這里會(huì)先用到我們下一章將講到的路由組件 ViewStack,這里僅需要了解如何使用即可。組件 ViewStack 對(duì)外只顯示子級(jí)的一個(gè)子組件,同時(shí)偵聽(tīng)一個(gè) switch 事件,該事件的派發(fā)者攜帶了一個(gè)切換到的目標(biāo)對(duì)象的名稱(chēng),也就是 ID。該組件根據(jù)這個(gè) ID 來(lái)切換到目標(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">松開(kāi)刷新</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 接口用戶(hù)設(shè)置與獲取組件的顯示狀態(tài)。父級(jí)組件可根據(jù)不同的時(shí)機(jī)調(diào)用該接口。

最終實(shí)現(xiàn)

有了上面的儲(chǔ)備,讓我們來(lái)填充完下拉刷新組件的細(xì)節(jié)。下拉刷新過(guò)程中會(huì)涉及到動(dòng)畫(huà),對(duì)于動(dòng)畫(huà)目前一般有兩種選擇,可以使用 JQuery 動(dòng)畫(huà)函數(shù),也可以是 css3,這需要看各人喜好了。這里我們選擇使用 css3 來(lái)實(shí)現(xiàn)。為清晰起見(jià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);
  }
 }
}

對(duì)于稍微有點(diǎn)復(fù)雜的組件,需要注意組件的組織歸類(lèi),盡量把具有相近功能的組件放在一起。為了便于敘述,上述所列出的組件示意總把它們視作是同一目錄,這一點(diǎn)讀者應(yīng)該能看出來(lái)。

本系列文章基于 xmlplus 框架。如果你對(duì) xmlplus 沒(méi)有多少了解,可以訪問(wèn) www.xmlplus.cn。這里有詳盡的入門(mén)文檔可供參考。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論