AMP
  • 故事

含結果的互動式投票

簡介

amp-story-interactive 元件為 Web Stories (網路故事) 提供可自訂的沉浸式測驗和投票。

在本指南中,我們將建置一個包含投票的 Web Story (網路故事),並將答案回報至結果元素。結果元素會根據使用者的投票答案顯示不同的類別。

首先,匯入 amp-story-interactive 以及您的其他故事匯入項目。

<script async custom-element="amp-story-interactive" src="https://cdn.ampproject.org/v0/amp-story-interactive-0.1.js"></script>

總覽

我們將建立一個互動式體驗,計算出您是什麼動物。option-{1/2/3/4}-results-category 屬性將每個投票選項連結到一個結果類別,因此請特別注意它。

定義投票

若要新增投票,我們必須指定一些屬性

  • option-{1/2/3/4}-text 傳遞選項至投票。

  • option-{1/2/3/4}-confetti 傳遞在選取選項時會爆炸的表情符號。選取一個選項試試看!

  • prompt-textprompt-size 決定問題的內容和大小。由於內容簡短,我們將其設為 large (大型)。

  • themechip-style 變更投票的外觀。

  • option-{1/2/3/4}-results-category 協助計算 amp-story-interactive-results 元素的狀態,詳情請參閱下方。

在此範例中,我們新增了一個深色透明投票,其中包含橘色的 prompt (提示) 文字,並且我們將每個選項連結到 option-{1/2/3/4}-results-category

<amp-story-page id="page-poll-1">
  <amp-story-grid-layer template="fill" style="background-color: white">
    <amp-img style="opacity:0.7"
      layout="fixed" width="1600" height="1200"
      src="https://images.unsplash.com/photo-1490818387583-1baba5e638af?q=20"
      alt="..." />
  </amp-story-grid-layer>
  <amp-story-grid-layer template="fill" aspect-ratio="4:6">
    <div animate-in="scale-fade-up">
      <amp-story-interactive-poll
      id="poll-1" class="center"
      prompt-text="What's your favorite food?"
      endpoint="https://amp.dev.org.tw/documentation/examples/components/amp-story-interactive-poll/results"
      prompt-size="large" theme="dark" chip-style="transparent"
      option-1-text="Bones" option-1-results-category="Dog" option-1-confetti="🦴"
      option-2-text="Fish" option-2-results-category="Cat" option-2-confetti="🐟"
      option-3-text="Carrots" option-3-results-category="Bunny" option-3-confetti="🥕"
      option-4-text="Cheese" option-4-results-category="Mouse" option-4-confetti="🧀">
    </amp-story-interactive-poll>
    </div>
  </amp-story-grid-layer>
</amp-story-page>

現在我們可以建立另一個具有相同屬性的投票。請注意,與每個選項相關聯的 results-category 與順序無關。對於此投票,我們將 accent color (輔色) 設定為綠色:--interactive-accent-color:rgb(9, 63, 4);

<amp-story-page id="page-poll-2">
  <amp-story-grid-layer template="fill">
    <amp-img
      layout="fixed" width="1600" height="1200"
      src="https://images.unsplash.com/photo-1542144582-1ba00456b5e3?q=20"
      alt="..." />
  </amp-story-grid-layer>
  <amp-story-grid-layer template="fill" aspect-ratio="4:6">
    <div animate-in="scale-fade-up">
      <amp-story-interactive-poll
        id="poll-2"
        prompt-text="How do you spend your free time?"
        endpoint="https://amp.dev.org.tw/documentation/examples/components/amp-story-interactive-poll/results"
        class="center"
        prompt-size="large"
        option-1-text="Stretching back" option-1-results-category="Cat" option-1-confetti="🧘"
        option-2-text="Brushing mustache" option-2-results-category="Mouse" option-2-confetti="👺"
        option-3-text="Jumping" option-3-results-category="Bunny" option-3-confetti="🦘"
        option-4-text="Fetching balls" option-4-results-category="Dog" option-4-confetti="⚽">
      </amp-story-interactive-poll>
    </div>
  </amp-story-grid-layer>
</amp-story-page>

讓我們再製作第三個,以訓練肌肉記憶。

創作者可以使用多種屬性和 CSS 變數來調整樣式,以符合故事設計。例如,在 prompt-background (提示背景) 中新增漸層是增強元素視覺效果的好方法。

--interactive-prompt-background: linear-gradient(120deg, var(--color-1), var(--color-2));

如果您想查看所有可用的樣式,請查看文件範例故事

<amp-story-page id="page-poll-3">
  <amp-story-grid-layer template="fill">
    <amp-img
      layout="fixed" width="1600" height="1200"
      src="https://images.unsplash.com/photo-1500835556837-99ac94a94552?q=20"
      alt="..." />
  </amp-story-grid-layer>
  <amp-story-grid-layer template="fill" aspect-ratio="4:6">
    <div animate-in="scale-fade-up">
      <amp-story-interactive-poll
         id="poll-3"
         prompt-text="What would you do for holidays?"
         endpoint="https://amp.dev.org.tw/documentation/examples/components/amp-story-interactive-poll/results"
         class="center poll-3"
         prompt-size="large"
         chip-style="shadow"
         option-1-text="Easter island" option-1-results-category="Bunny" option-1-confetti="🗿"
         option-2-text="Drive with tongue out" option-2-results-category="Dog" option-2-confetti="🏎️"
         option-3-text="Run from cats" option-3-results-category="Mouse" option-3-confetti="🏃"
         option-4-text="Stay in bed" option-4-results-category="Cat" option-4-confetti="😴">
      </amp-story-interactive-poll>
    </div>
  </amp-story-grid-layer>
</amp-story-page>

我們已在所有投票中指定 option-{1/2/3/4}-results-category,但沒有任何立即效果,但它們將在此處發揮作用。

內部狀態會儲存使用者的投票選取項目。amp-story-interactive-results 元素會讀取每個投票狀態並計算結果。然後,它會顯示已計算狀態的類別、描述和圖片。

如果您回到其他頁面並回答投票 (在下方故事中),您會看到當您回到結果頁面時,結果如何將狀態變更為另一種動物。

顯示的結果將是選取選項最多的結果,並優先顯示首先設定的狀態。如果未選取任何選項,則預設會顯示 option-1 狀態。

<amp-story-page id="page-results">
  <amp-story-grid-layer template="fill">
    <amp-img
      layout="fixed" width="1600" height="1200"
      src="https://images.unsplash.com/photo-1500835556837-99ac94a94552?q=20"
      alt="..." />
  </amp-story-grid-layer>
  <amp-story-grid-layer template="fill" aspect-ratio="4:6">
    <div animate-in="scale-fade-up">
      <amp-story-interactive-results
         id="results-1" class="center"
         prompt-text="You are a"
         option-1-results-category="Cat" option-1-text="Everyone loves cats, and so do you! Cats are the best companion to WFH" option-1-image="https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg"
         option-2-results-category="Dog" option-2-text="You always have energy and love being with friends. Outdoors is your favorite place" option-2-image="https://images.pexels.com/photos/1108099/pexels-photo-1108099.jpeg"
         option-3-results-category="Bunny" option-3-text="You love jumping around and having fun. Your bubbly personality is contagious, and your love for carrots is unconditional" option-3-image="https://images.pexels.com/photos/326012/pexels-photo-326012.jpeg"
         option-4-results-category="Mouse" option-4-text="Even though you're small, you're also full of curiosity and love going around finding new things to do" option-4-image="https://images.pexels.com/photos/51340/rat-pets-eat-51340.jpeg">
      </amp-story-interactive-results>
    </div>
  </amp-story-grid-layer>
</amp-story-page>

更多資源

如果您想進一步瞭解 Web Stories (網路故事) 的所有互動式體驗,請前往文件,或查看關於如何製作包含測驗的故事的指南。

需要進一步說明嗎?

如果本頁面上的說明未涵蓋您的所有問題,請隨時與其他 AMP 使用者聯絡,討論您的確切使用案例。

前往 Stack Overflow
未說明的特色功能?

AMP 專案強烈鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎針對您特別感興趣的問題提供一次性貢獻。

在 GitHub 上編輯範例