带结果的互动测验
简介
amp-story-interactive
组件为 Web Stories 提供可自定义的沉浸式测验和投票。
在本指南中,我们将构建一个带有测验的 Web Story,该测验将答案报告给结果元素。结果元素会根据正确的测验答案显示不同的类别。
首先,将 amp-story-interactive
与其他 Story 导入一起导入。
<script async custom-element="amp-story-interactive" src="https://cdn.ampproject.org/v0/amp-story-interactive-0.1.js"></script>
我们还将链接分析以跟踪统计数据
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
将您的 amp-analytics 配置添加到 Story 中。我们将使用 ${storyInteractiveId}
和 ${storyInteractiveResponse}
来收集有关响应的更多信息。完整的变量列表可以在文档中找到。
<amp-analytics id="my-analytics">
<script type="application/json">
{
"requests": {
"interactive": "https://example.com/interactive?id=${storyInteractiveId}&response=${storyInteractiveResponse}"
},
"triggers": {
"interactiveSelected": {
"on": "story-interactive",
"request": "interactive"
}
}
}
</script>
</amp-analytics>
概述
我们将创建一个互动体验,告诉你你对足球的了解程度。
为此,我们将使用一个 amp-story-interactive-results
元素来显示用户的测验结果。此元素向用户显示正确率百分比和相应的类别。
定义测验
要添加测验,我们必须指定一些属性
option-{1/2/3/4}-text
将用于将选项传递给投票option-{1/2/3/4}-confetti
将用于传递在选择选项时爆炸的表情符号。您应该只给正确的选项提供彩带,因为我们不想在选择的选项不正确时触发动画。试一试并选择“乌拉圭”,看看效果如何!prompt-text
和prompt-size
将决定问题的内容和大小。由于内容简短,我们将使其变大。theme
和chip-style
将更改投票的外观。option-{1/2/3/4}-correct
用于显示哪个选项是正确的。
在这种情况下,我们添加了一个带有绿色提示文本的透明测验,并启用了 option-3-correct
。
<amp-story-page id="page-quiz-1">
<amp-story-grid-layer template="fill" style="background-color: black">
<amp-img style="opacity:0.7"
layout="fixed" width="1600" height="1200"
src="https://images.unsplash.com/photo-1508098682722-e99c43a406b2?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-quiz
id="quiz-1" class="center"
prompt-text="Who won the first soccer world cup?"
endpoint="https://amp.org.cn/documentation/examples/components/amp-story-interactive-poll/results"
prompt-size="large" chip-style="transparent"
option-1-text="Spain"
option-2-text="Brazil"
option-3-text="Uruguay" option-3-correct option-3-confetti="⚽"
option-4-text="Germany">
</amp-story-interactive-quiz>
</div>
</amp-story-grid-layer>
</amp-story-page>
现在我们可以创建另一个具有相同属性的测验。对于此测验,我们将强调颜色设置为绿色:--interactive-accent-color:rgb(9, 63, 4);
,这将用它来着色提示背景和测验强调色。在这种情况下,我们将不使用透明样式,因为背景更加详细。
<amp-story-page id="page-quiz-2">
<amp-story-grid-layer template="fill">
<amp-img
layout="fixed" width="1600" height="1200"
src="https://images.unsplash.com/photo-1527871252447-4ce32da643c6?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-quiz
id="quiz-2"
prompt-text="What country won the most soccer world cups?"
endpoint="https://amp.org.cn/documentation/examples/components/amp-story-interactive-poll/results"
class="center"
prompt-size="large"
option-1-text="Spain"
option-2-text="Brazil" option-2-correct option-2-confetti="⚽"
option-3-text="Uruguay"
option-4-text="Germany">
</amp-story-interactive-quiz>
</div>
</amp-story-grid-layer>
</amp-story-page>
让我们再做第三个来训练肌肉记忆。
例如,在提示背景中添加渐变是增强元素视觉效果的好方法。
--interactive-prompt-background: linear-gradient(120deg, var(--color-1), var(--color-2));
<amp-story-page id="page-quiz-3">
<amp-story-grid-layer template="fill">
<amp-img
layout="fixed" width="1600" height="1200"
src="https://images.unsplash.com/photo-1531266752426-aad472b7bbf4?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-quiz
id="quiz-3"
prompt-text="Where are FIFA headquarters?"
endpoint="https://amp.org.cn/documentation/examples/components/amp-story-interactive-poll/results"
class="center"
prompt-size="large"
option-1-text="Germany"
option-2-text="USA"
option-3-text="Switzerland" option-3-correct option-3-confetti="🌐"
option-4-text="Spain">
</amp-story-interactive-quiz>
</div>
</amp-story-grid-layer>
</amp-story-page>
链接到结果
我们必须在 amp-story-interactive-results
中为每个状态配置阈值。这使其可以根据用户的测验选择计算并显示结果。
当用户回答测验时,内部状态会保存正确答案的百分比,并检查哪个 results-threshold
适合计算出的百分比。然后,它会显示计算出的状态的类别、描述和图像。
如果您返回到其他页面并回答测验(在下面的 Story 中),您会看到当您返回到结果页面时,结果状态和显示的百分比会如何变化。在这里,我们创建了 2 个状态:“专家”表示正确回答所有问题的用户,“粉丝”表示错过一个或多个问题的用户。尝试在下面的 Story 中正确回答所有问题,并查看该元素如何更改以显示“专家”类别。
amp-story-interactive-results
元素上添加最多 4 个状态,并为不同的状态添加图像。有关带有图像的结果元素的示例,请查看有关如何制作带有投票的 Web Story的指南<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-1527871369852-eb58cb2b54e2?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 soccer"
option-1-results-category="Fan" option-1-text="You might not be the most knowledgeable, but enjoy watching games every now and then" option-1-results-threshold="0"
option-2-results-category="Expert" option-2-text="You know everything about soccer, be proud of it!" option-2-results-threshold="80">
</amp-story-interactive-results>
</div>
</amp-story-grid-layer>
</amp-story-page>
更多资源
如果您想了解更多有关 Web Story 的所有互动体验,请前往文档或查看有关如何制作带有投票的 Story的指南。
如果此页面上的解释没有涵盖您的所有问题,请随时与其他 AMP 用户联系以讨论您的确切用例。
转到 Stack Overflow 未解释的功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您成为我们开源社区的持续参与者,但也欢迎您对您特别感兴趣的问题做出一次性贡献。
在 GitHub 上编辑示例-
由 @mszylkowski 撰写