溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

xmlplus組件如何實現(xiàn)下拉刷新

發(fā)布時間:2021-08-13 16:30:12 來源:億速云 閱讀:85 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)xmlplus組件如何實現(xiàn)下拉刷新,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

如何實現(xiàn)一個簡單的下拉刷新組件。

xmlplus組件如何實現(xiàn)下拉刷新

目標(biāo)組件分析

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

Example1: {
 xml: `<PullRefresh id='example'>
    <h2>Twitter</h2>
    <h3>Loren Brichter</h3>
   </PullRefresh>`,
 fun: function (sys, items, opts) {
  sys.example.on("ready", () => console.log("ready"));
 }
}

示例中的使用方式是非常簡潔的,但我們還漏了一點。如果你用過一些新聞客戶端,在某些情況下,此客戶端會自動觸發(fā)下拉刷新操作。比如,剛進(jìn)入客戶端頁面或者由于軟件推送機(jī)制產(chǎn)生的被動列表更新,這都將導(dǎo)致客戶端下拉刷新操作的觸發(fā)。所以如上的 PullRefresh 組件還應(yīng)該提供一個觸發(fā)自動刷新的操作接口。好了,下面是加入下拉刷新接口的應(yīng)用示例。

Example2: {
 xml: `<PullRefresh id='example'>
    <h2>Twitter</h2>
    <h3>Loren Brichter</h3>
    <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ù)不同的時機(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);
  }
 }
}

關(guān)于“xmlplus組件如何實現(xiàn)下拉刷新”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI