進度條

除了 預載器,Framework7 還附帶精美的動畫確定和無限/不確定進度條,以指示活動。

確定進度條

當進度條確定時,它會指示操作將花費多長時間,當完成的百分比可偵測時。

讓我們看看確定進度條的配置

<div class="progressbar" data-progress="20">
  <span></span>
</div>

其中 data-progress="20" - 目前進度(以百分比表示)。請注意,此資料屬性僅在載入頁面時設定進度。如果您需要稍後變更,應透過 API 進行。

無限進度條

當進度條無限/不確定時,它會要求使用者在完成某件事時等待,而無需指示需要花費多長時間。

讓我們看看無限進度條的配置

<div class="progressbar-infinite"></div>

進度條顏色

進度條支援所有 預設顏色。因此,要變更其顏色,只需將 color-[color] 類別新增至進度條元素即可。

<!-- Red progressbar -->
<div class="progressbar color-red" data-progress="20">
  <span></span>
</div>

<!-- Green progressbar -->
<div class="progressbar color-green" data-progress="50">
  <span></span>
</div>

<!-- Yellow infinite progressbar -->
<div class="progressbar-infinite color-yellow"></div>

<!-- Multicolor infinite progressbar -->
<div class="progressbar-infinite color-multi"></div>

進度條 API

進度條附帶 API,讓您可以控制進度條的進度,顯示和隱藏它。讓我們看看適用的 App 屬性和方法

app.progressbar.set(el, progress, duration) - 設定確定進度條的進度。

  • el - 字串HTMLElement。進度條元素或包含進度條元素的元素。如果是字串 - 該元素的 CSS 選擇器。
  • progress - 數字。進度(以百分比表示,介於 0 至 100 之間)
  • duration - 數字。進度變更動畫的過渡時間(以毫秒為單位)
  • 此方法傳回進度條 HTMLElement

app.progressbar.set(progress, duration) - 設定位於 app 根元素下的確定進度條的進度。

  • progress - 數字。進度(以百分比表示,介於 0 至 100 之間)
  • duration - 數字。進度變更動畫的過渡時間(以毫秒為單位)
  • 此方法傳回進度條 HTMLElement

app.progressbar.show(el, progress, color) - 建立並顯示或僅顯示(如果已顯示)進度條。

  • el - 字串HTMLElement。進度條元素容器或包含進度條元素的元素。如果是字串 - 該元素的 CSS 選擇器。選用
  • progress - 數字。進度(以百分比表示,介於 0 至 100 之間)。選用
  • color - 字串。進度條的顏色,例如「白色」、「紅色」等,來自可用的 色彩主題選用
  • 此方法傳回進度條 HTMLElement

這裡的所有參數都是選用的

  • 如果您省略 el 參數,它會在 app 根目錄下尋找(或建立)進度條元素
  • 如果您省略 progress,它會顯示/建立無限進度條
  • 如果您省略所有參數,它將在應用程式根目錄下顯示/建立預設顏色的無限進度條

app.progressbar.hide(el) - 隱藏進度條。

  • el - 字串HTMLElement。進度條元素容器或包含進度條元素的元素。如果為字串 - 該元素的 CSS 選擇器。如果未指定,它將在應用程式根元素下尋找此類元素。

CSS 變數

以下是相關 CSS 變數(CSS 自訂屬性)清單。

請注意,註解的變數預設未指定,而它們的值是它們在此情況下的後備值。

:root {
  /*
  --f7-progressbar-progress-color: var(--f7-theme-color);
  */
}
.ios {
  --f7-progressbar-height: 4px;
  --f7-progressbar-border-radius: 4px;
  --f7-progressbar-bg-color: rgba(0, 0, 0, 0.3);
}
.ios .dark,
.ios.dark {
  --f7-progressbar-bg-color: rgba(255, 255, 255, 0.3);
}
.md {
  --f7-progressbar-height: 4px;
  --f7-progressbar-border-radius: 0px;
}
.md,
.md .dark,
.md [class*='color-'] {
  --f7-progressbar-bg-color: rgba(var(--f7-theme-color-rgb), 0.5);
}

範例

progressbar.html
<template>
  <div class="page">
    <div class="navbar">
      <div class="navbar-bg"></div>
      <div class="navbar-inner sliding">
        <div class="title">Progress Bar</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block">
        <p>In addition to <a href="/preloader/">Preloader</a>, Framework7 also comes with fancy animated determinate and
          infinite/indeterminate progress bars to indicate some activity.</p>
      </div>
      <div class="block-title">Determinate Progress Bar</div>
      <div class="block block-strong-ios block-outline-ios">
        <p>When progress bar is determinate it indicates how long an operation will take when the percentage complete is
          detectable.</p>
        <p>Inline determinate progress bar:</p>
        <div>
          <p><span data-progress="10" class="progressbar" id="demo-inline-progressbar"></span></p>
          <p class="segmented segmented-raised">
            <a class="button" @click=${()=> setInlineProgress(10)}>10%</a>
            <a class="button" @click=${()=> setInlineProgress(30)}>30%</a>
            <a class="button" @click=${()=> setInlineProgress(50)}>50%</a>
            <a class="button" @click=${()=> setInlineProgress(100)}>100%</a>
          </p>
        </div>
        <div>
          <p>Inline determinate load & hide:</p>
          <p id="demo-determinate-container"></p>
          <p>
            <a href="" class="button button-fill" @click=${()=> showDeterminate(true)}>Start Loading</a>
          </p>
        </div>
        <div>
          <p>Overlay with determinate progress bar on top of the app:</p>
          <p>
            <a href="" class="button button-fill" @click=${()=> showDeterminate(false)}>Start Loading</a>
          </p>
        </div>
      </div>
      <div class="block-title">Infinite Progress Bar</div>
      <div class="block block-strong-ios block-outline-ios">
        <p>When progress bar is infinite/indeterminate it requests that the user wait while something finishes when it’s
          not necessary to indicate how long it will take.</p>
        <p>Inline infinite progress bar</p>
        <p>
          <span class="progressbar-infinite"></span>
        </p>
        <p>Multi-color infinite progress bar</p>
        <p>
          <span class="progressbar-infinite color-multi"></span>
        </p>
        <div>
          <p>Overlay with infinite progress bar on top of the app</p>
          <p id="demo-infinite-container"></p>
          <p>
            <a href="" class="button button-fill" @click=${()=> showInfinite(false)}>Start Loading</a>
          </p>
        </div>
        <div>
          <p>Overlay with infinite multi-color progress bar on top of the app</p>
          <p>
            <a href="" class="button button-fill" @click=${()=> showInfinite(true)}>Start Loading</a>
          </p>
        </div>
      </div>
      <div class="block-title">Colors</div>
      <div class="list list-strong-ios list-outline-ios list-dividers-ios simple-list">
        <ul>
          <li>
            <div class="progressbar color-blue" data-progress="10"></div>
          </li>
          <li>
            <div class="progressbar color-red" data-progress="20"></div>
          </li>
          <li>
            <div class="progressbar color-pink" data-progress="30"></div>
          </li>
          <li>
            <div class="progressbar color-green" data-progress="80"></div>
          </li>
          <li>
            <div class="progressbar color-yellow" data-progress="90"></div>
          </li>
          <li>
            <div class="progressbar color-orange" data-progress="100"></div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
  export default (props, { $f7, $el, $onMounted, $onBeforeUnmount }) => {
    let determinateLoading = false;
    let infiniteLoading = false;
    const setInlineProgress = (value) => {
      $f7.progressbar.set('#demo-inline-progressbar', value);
    }
    const showDeterminate = (inline) => {
      if (determinateLoading) return;
      determinateLoading = true;
      var progressBarEl;
      if (inline) {
        progressBarEl = $f7.progressbar.show('#demo-determinate-container', 0);
      } else {
        progressBarEl = $f7.progressbar.show(0);
      }
      var progress = 0;
      function simulateLoading() {
        setTimeout(function () {
          var progressBefore = progress;
          progress += Math.random() * 20;
          $f7.progressbar.set(progressBarEl, progress);
          if (progressBefore < 100) {
            simulateLoading(); //keep "loading"
          }
          else {
            determinateLoading = false;
            $f7.progressbar.hide(progressBarEl); //hide
          }
        }, Math.random() * 200 + 200);
      }
      simulateLoading();
    }
    const showInfinite = (multiColor) => {
      if (infiniteLoading) return;
      infiniteLoading = true;
      if (multiColor) {
        $f7.progressbar.show('multi');
      } else {
        $f7.progressbar.show();
      }
      setTimeout(function () {
        infiniteLoading = false;
        $f7.progressbar.hide();
      }, 3000);
    }

    return $render;
  };
</script>