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>
活动
监听 select
或 selectedOptions
事件,以触发其他操作或更新 amp-state
。
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
的绑定更容易实现。
<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 上编辑示例-
由 @sebastianbenz 编写