amp-autocomplete根据用户键入时给出的输入建议完整结果。这是一个非常常见且有用的函数,因为它使用户可以更快地完成任务。当用户不知道输入的全部范围时,这非常有用。
必需的脚本:导入amp-autocomplete组件-
HTML
<script async custom-element="amp-autocomplete" src=
"https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js">
</script>
导入amp-form组件-
HTML
<script async custom-element="amp-form" src=
"https://cdn.ampproject.org/v0/amp-form-0.1.js">
</script>
用法:
- amp-autocomplete必须嵌套在表单中。
- 表单应包含带有输入标签或textarea标签的输入字段。
- 数据源必须是包含数组属性项的JSON对象。
- 可以使用子脚本type =“ application /json”标签指定数据源,也可以通过src标签指定数据源。
使用子脚本type =” application /json”-
HTML
<form class="sample-form" method="post"
action-xhr=
"https://amp.dev/documentation/examples/api/echo"
target="_top">
<amp-autocomplete filter="substring">
<input>
<script type="application/json">
{
"items":["geeksforgeeks", "gfg",
"iamageek" ,"how to become a geek?"]
}
</script>
</amp-autocomplete>
</form>
使用src标记-
HTML
<form class="sample-form" method="post"
action-xhr=
"https://amp.dev/documentation/examples/api/echo"
target="_top">
<!--the following src link contains
names of cities-->
<amp-autocomplete filter="substring"
src="/static/samples/json/amp-autocomplete-cities.json">
<input>
</amp-autocomplete>
</form>
范例1:
HTML
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<link rel="canonical" href=
"https://amp.dev/documentation/examples/components/amp-autocomplete/index.html">
<meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">
<script async custom-element=
"amp-autocomplete" src=
"https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js">
</script>
<script async custom-element=
"amp-form" src=
"https://cdn.ampproject.org/v0/amp-form-0.1.js">
</script>
<title>Google AMP amp-autocomplete</title>
<style amp-custom>
:root {
--space-1:.5rem;
/* 8px */
--space-2:1rem;
/* 16px */
--space-3:1.5rem;
/* 24px */
--space-4:2rem;
/* 32px */
}
.sample-form input[type=submit] {
-webkit-appearance:none;
border:none;
background-color:var(--color-primary);
color:var(--color-text-light);
margin:var(--space-2);
font-family:'Poppins', sans-serif;
font-weight:700;
line-height:1.2em;
font-size:1em;
padding:0.75em 1.75em;
text-decoration:none;
text-align:center;
border-radius:3px;
border:none;
box-shadow:var(--box-shadow-1);
}
.sample-form {
padding:var(--space-2);
}
</style>
<style amp-boilerplate>
body {
-webkit-animation:-amp-start 8s
steps(1, end) 0s 1 normal both;
-moz-animation:-amp-start 8s
steps(1, end) 0s 1 normal both;
-ms-animation:-amp-start 8s
steps(1, end) 0s 1 normal both;
animation:-amp-start 8s
steps(1, end) 0s 1 normal both
}
@-webkit-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@-moz-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@-ms-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@-o-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
</style>
<noscript>
<style amp-boilerplate>
body {
-webkit-animation:none;
-moz-animation:none;
-ms-animation:none;
animation:none
}
</style>
</noscript>
</head>
<body>
<form class="sample-form" method="post"
action-xhr=
"https://amp.dev/documentation/examples/api/echo"
target="_top">
<amp-autocomplete filter="substring">
<input>
<!-- Auto suggestion strings
stored in item-->
<script type="application/json">
{
"items":["geeksforgeeks",
"gfg", "iamageek",
"how to become a geek?"]
}
</script>
</amp-autocomplete>
</form>
</body>
</html>
输出
范例2:
HTML
<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<link rel="canonical" href=
"https://amp.dev/documentation/examples/components/amp-autocomplete/index.html">
<meta name="viewport" content=
"width=device-width,minimum-scale=1,initial-scale=1">
<script async custom-element=
"amp-autocomplete" src=
"https://cdn.ampproject.org/v0/amp-autocomplete-0.1.js">
</script>
<script async custom-element=
"amp-form" src=
"https://cdn.ampproject.org/v0/amp-form-0.1.js">
</script>
<title>Google AMP amp-autocomplete</title>
<style amp-custom>
:root {
--space-1:.5rem;
/* 8px */
--space-2:1rem;
/* 16px */
--space-3:1.5rem;
/* 24px */
--space-4:2rem;
/* 32px */
}
.sample-form input[type=submit] {
-webkit-appearance:none;
border:none;
background-color:var(--color-primary);
color:var(--color-text-light);
margin:var(--space-2);
font-family:'Poppins', sans-serif;
font-weight:700;
line-height:1.2em;
font-size:1em;
padding:0.75em 1.75em;
text-decoration:none;
text-align:center;
border-radius:3px;
border:none;
box-shadow:var(--box-shadow-1);
}
.sample-form {
padding:var(--space-2);
}
</style>
<style amp-boilerplate>
body {
-webkit-animation:-amp-start 8s
steps(1, end) 0s 1 normal both;
-moz-animation:-amp-start 8s
steps(1, end) 0s 1 normal both;
-ms-animation:-amp-start 8s
steps(1, end) 0s 1 normal both;
animation:-amp-start 8s
steps(1, end) 0s 1 normal both
}
@-webkit-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@-moz-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@-ms-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@-o-keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
@keyframes -amp-start {
from {
visibility:hidden
}
to {
visibility:visible
}
}
</style>
<noscript>
<style amp-boilerplate>
body {
-webkit-animation:none;
-moz-animation:none;
-ms-animation:none;
animation:none
}
</style>
</noscript>
</head>
<body>
<form class="sample-form" method="post"
action-xhr=
"https://amp.dev/documentation/examples/api/echo"
target="_top">
<!--the following src link contains
names of cities-->
<amp-autocomplete filter="substring"
src="/static/samples/json/amp-autocomplete-cities.json">
<input>
</amp-autocomplete>
</form>
</body>
</html>
输出
相关用法
- Google AMP amp-ad用法及代码示例
- Google AMP amp-accordion用法及代码示例
- Google AMP amp-facebook-like用法及代码示例
- Google AMP amp-carousel用法及代码示例
- Google AMP amp-date-countdown用法及代码示例
- Google AMP amp-brightcove用法及代码示例
- Google AMP amp-bind-recaptcha用法及代码示例
- Google AMP amp-img用法及代码示例
- Google AMP amp-image-lightbox用法及代码示例
- Google AMP amp-lightbox-gallery用法及代码示例
- Google AMP amp-soundcloud用法及代码示例
- Google AMP amp-mustache用法及代码示例
注:本文由纯净天空筛选整理自somsagar2019大神的英文原创作品 Google AMP amp-autocomplete。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。