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

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

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

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

目標(biāo)組件分析

和前面在設(shè)計組件時的做法一樣,我們先想想看最終的成品組件是如何使用的,這需要點想像力。下拉刷新組件看成一個容器組件是合理的,用戶可以對容器的內(nèi)容進(jìn)行下拉操作。如果用戶完成了完整的下拉觸發(fā)操作,該組件應(yīng)該會有下拉完成的事件反饋,假定這個事件名為 ready。根據(jù)以上的分析,我們很有可能得到下面的一個該組件的應(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"));
 }
}

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

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

外圍 div 元素包含兩個子組件:其中一個是狀態(tài)指示條,用于顯示“下拉刷新”、“松開刷新”、“加載中...”以及“刷新成功”四個狀態(tài)提示,這里暫時使用未定義的 Status 組件替代;另一個 div 元素用于容納下拉刷新組件的包含內(nèi)容。到現(xiàn)在,大概可以想得出該組件的工作邏輯了,于是我們可以給出下面的一個基本的組件框架。

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)面跟隨觸點移動
   // 2 根據(jù)觸點移動的距離顯示相當(dāng)?shù)臓顟B(tài)條內(nèi)容
  }
  function touchend(e) {
   // 1 移除 touchmove 和 touchend 事件
   // 2 根據(jù)觸點移動的距離決定返回原始狀態(tài)或者進(jìn)入刷新狀態(tài)并派發(fā)事件
  }
 }
}

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

如前面提到的,狀態(tài)條組件包含四個狀態(tài)提示,并且每一時刻僅顯示一個狀態(tài)。對于狀態(tài)的切換,這里會先用到我們下一章將講到的路由組件 ViewStack,這里僅需要了解如何使用即可。組件 ViewStack 對外只顯示子級的一個子組件,同時偵聽一個 switch 事件,該事件的派發(fā)者攜帶了一個切換到的目標(biāo)對象的名稱,也就是 ID。該組件根據(jù)這個 ID 來切換到目標(biāo)視圖。下面是狀態(tài)條組件的完整實現(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 });
 }
}

該組件提供一個 value 接口用戶設(shè)置與獲取組件的顯示狀態(tài)。父級組件可根據(jù)不同的時機調(diào)用該接口。

最終實現(xiàn)

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

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

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

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

相關(guān)文章

最新評論