AMP
  • 网站

amp-selector

简介

amp-selector 允许用户从选项列表中选择。选项的内容不限于文本。

设置

导入 amp-selector 组件。

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

此示例还演示了如何将 amp-selector 与表单结合使用...

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

...以及 amp-bind

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

单选

amp-selector 默认设置为单选。所有可选元素都需要 option,例如,以下示例中的分隔符无法被选中。

<amp-selector class="sample-selector" layout="container">
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2"></amp-img>
  <div class="divider inline-block mx1"></div>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4"></amp-img>
</amp-selector>

多选

添加 multiple 属性以启用多选。

<amp-selector layout="container" class="sample-selector" multiple>
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2"></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4"></amp-img>
</amp-selector>

禁用和预选属性

以下属性受选择元素支持

  • disabled:禁用元素。
  • selected:预选元素。
<amp-selector layout="container" class="sample-selector" multiple>
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2" selected></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4" disabled></amp-img>
</amp-selector>

已禁用的 amp-selector

添加 disabled 属性以禁用整个选择器。

<amp-selector layout="container" class="sample-selector" disabled>
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>

键盘选择模式

添加 keyboard-select-mode 属性以选择 amp-selector 的预定义用户交互模式。默认情况下,该值为“NONE”,并显示以下行为。

<amp-selector layout="container" keyboard-select-mode="none" class="sample-selector" disabled>
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>

键盘选择模式:焦点

添加 keyboard-select-mode="focus" 属性,以便 Tab 键将焦点移至 amp-selector。用户使用箭头键在项目之间导航。必须按空格键或 Enter 键才能更改选择。

<amp-selector layout="container" keyboard-select-mode="focus" class="sample-selector">
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>

键盘选择模式:选择

添加 keyboard-select-mode="focus" 属性,以便 Tab 键将焦点移至 amp-selector。当用户使用箭头键导航选项时,选择会发生变化。

<amp-selector layout="container" keyboard-select-mode="select" class="sample-selector">
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>

操作

可以使用 clear 操作清除 amp-selector 的所有选择。

<amp-selector id="actionsSample" layout="container" class="sample-selector" multiple>
  <amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
  <amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2"></amp-img>
  <amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3" selected></amp-img>
  <amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4"></amp-img>
  <amp-img src="/static/samples/img/landscape_trees_300x225.jpg" width="90" height="60" option="5" selected></amp-img>
  <amp-img src="/static/samples/img/landscape_green_300x200.jpg" width="90" height="60" option="6"></amp-img>
</amp-selector>
<button on="tap:actionsSample.clear">clear</button>
<button on="tap:actionsSample.selectUp">selectUp</button>
<button on="tap:actionsSample.selectUp(delta=2)">selectUp(delta=2)</button>
<button on="tap:actionsSample.selectDown">selectDown</button>
<button on="tap:actionsSample.selectDown(delta=2)">selectDown(delta=2)</button>
<button on="tap:actionsSample.toggle(index=1)">toggle(index=1)</button>
<button on="tap:actionsSample.toggle(index=1, value=true)">toggle(index=1, value=true)</button>

活动

监听 selectselectedOptions 事件以触发其他操作或更新 amp-state

选项 A
选项 B
选项 C
选项 D
上次选择的选项:a
所有选定的选项:a
<amp-selector multiple layout="container" class="radio-selector" on="select: AMP.setState({
      selectedOption: event.targetOption,
      allSelectedOptions: event.selectedOptions
    })">
  <div option="a" selected>Option A</div>
  <div option="b">Option B</div>
  <div option="c">Option C</div>
  <div option="d">Option D</div>
</amp-selector>
<div>Last selected option:<code [text]="selectedOption">a</code></div>
<div>All selected option(s): <code [text]="allSelectedOptions.join(', ')">a</code></div>

表单

在表单内部,amp-selector 的行为类似于单选按钮/复选框组,并根据 amp-selector 的名称提交所选值。在这里,我们已经对 amp-selector 进行了样式设置,使其类似于单选列表。为此使用 amp-selector 的优点是使用 amp-bind 的绑定更容易实现。

选项 A
选项 B
选项 C
<form action="#" method="get" target="_top">
  <amp-selector class="radio-selector" layout="container" name="my-selector">
    <div option="a">Option A</div>
    <div option="b">Option B</div>
    <div option="c">Option C</div>
  </amp-selector>
  <button>Submit</button>
</form>
需要进一步解释吗?

如果此页面上的解释未能涵盖您的所有问题,请随时与其他 AMP 用户联系以讨论您的确切用例。

前往 Stack Overflow
未解释的功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您成为我们开源社区的持续参与者,但我们也欢迎您为特别感兴趣的问题做出一次性贡献。

在 GitHub 上编辑示例