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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。