AMP

重要提示:此文档不适用于您当前选择的格式广告

amp-date-countdown

 
您现在可以使用此组件在有效的 AMP 文档之外使用 此组件的 Bento 版本。 在 Bento 指南中了解更多信息。

描述

显示到指定日期的倒计时序列。

 

必需脚本

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

用法

显示一个向后计数的序列,以指示在计划发生事件之前剩余的时间。


amp-date-countdown 提供倒计时时间数据,您可以在 AMP 页面中呈现这些数据。 通过在 amp-date-countdown 标记中提供特定的属性amp-date-countdown 扩展会返回时间参数列表,您可以将这些参数传递给 amp-mustache 模板进行呈现。 请参阅下面的每个返回的时间参数的列表


<amp-date-countdown
  timestamp-seconds="2147483648"
  layout="fixed-height"
  height="50"
>
  <template type="amp-mustache">
    <p class="p1">
{{d}} days, {{h}} hours, {{m}} minutes and {{s}} seconds until
      <a href="https://en.wikipedia.org/wiki/Year_2038_problem">Y2K38</a>.
    </p>
  </template>
</amp-date-countdown>

返回的时间参数

此表列出了您可以在 Mustache 模板中指定的格式

格式 含义
d 天 - 0、1、2、...12、13..无穷大
dd 天 - 00、01、02、03..无穷大
h 小时 - 0、1、2、...12、13..无穷大
hh 小时 - 01、02、03..无穷大
m 分钟 - 0、1、2、...12、13..无穷大
mm 分钟 - 01、01、02、03..无穷大
s 秒 - 0、1、2、...12、13..无穷大
ss 秒 - 00、01、02、03..无穷大
days 天或多天的国际化字符串
hours 小时或多小时的国际化字符串
minutes 分钟或多分钟的国际化字符串
seconds 秒或多秒的国际化字符串

格式化值的示例

此表提供在 Mustache 模板中指定的格式化值的示例,以及输出示例

格式 示例输出 备注
{hh}:{mm}:{ss} 04:24:06 -
{h} {hours} 和 {m} {minutes} 和 {s} {seconds} 4 小时 1 分钟 45 秒 -
{d} {days} {h}:{mm} 1 天 5:03 -
{d} {days} {h} {hours} {m} {minutes} 50 天 5 小时 10 分钟 -
{d} {days} {h} {hours} {m} {minutes} 20 天 5 小时 10 分钟 -
{h} {hours} {m} {minutes} 240 小时 10 分钟 biggest-unit='hours'
{d} {days} {h} {hours} {m} {minutes} 50 天 5 小时 10 分钟 locale='zh-cn'

属性

您必须至少指定以下必需属性之一:end-datetimeleft-mstimestamp-mstimestamp-seconds

end-date

要倒计时的 ISO 格式日期。 例如,2020-06-01T00:00:00+08:00

timestamp-ms

以毫秒为单位的 POSIX 纪元值;假定为 UTC 时区。 例如,timestamp-ms="1521880470000"

timestamp-seconds

以秒为单位的 POSIX 纪元值;假定为 UTC 时区。 例如,timestamp-seconds="1521880470"

timeleft-ms

要倒计时的剩余毫秒值。 例如,剩余 48 小时 timeleft-ms="172800000"

offset-seconds(可选)

一个正数或负数,表示从 end-date 添加或减去的秒数。 例如,offset-seconds="60" 会在结束日期中添加 60 秒。

when-ended(可选)

指定计时器是否在到达 0 秒时停止。 该值可以设置为 stop(默认值),表示计时器在 0 秒时停止,并且不会超过最终日期,或者设置为 continue,表示计时器应在到达 0 秒后继续。

locale(可选)

每个计时器单元的国际化语言字符串。 默认值为 en(表示英语)。

支持的值

代码 语言
de 德语
en 英语
es 西班牙语
fr 法语
id 印度尼西亚语
it 意大利语
ja 日语
ko 韩语
nl 荷兰语
pt 葡萄牙语
ru 俄语
th 泰语
tr 土耳其语
vi 越南语
zh-cn 简体中文
zh-tw 繁体中文

biggest-unit(可选)

允许 amp-date-countdown 组件根据指定的 biggest-unit 值计算时间差。 例如,假设还剩下 50 天 10 小时,如果 biggest-unit 设置为 hours,则结果会显示还剩下 1210 小时

  • 支持的值:dayshoursminutesseconds
  • 默认值:days

data-count-up(可选)

包含此属性以反转倒计时的方向,改为向上计数。 这对于显示自过去目标日期以来经过的时间很有用。 要在目标日期在过去时继续倒计时,请务必包含 when-ended 属性并使用 continue 值。 如果目标日期在未来,amp-date-countdown 将显示一个递减(向 0)的负值。

活动

amp-date-countdown 组件公开以下事件,您可以使用AMP on-syntax 来触发

超时

计时器超时时。 要使此操作生效,when-ended必须设置为 stop。 计时器超时时,您只能运行低信任操作,例如 amp-animationamp-video 操作。 这是为了强制执行 AMP 的 UX 原则,即不允许在没有明确用户操作的情况下重新排布页面内容。 |

示例:演示超时事件的用法

<h1 id="sample">
  When Timer hits 0, will hide the timer itself and hide this message.
</h1>

<amp-animation id="hide-timeout-event" layout="nodisplay">
  <script type="application/json">
    {
      "duration": "1s",
      "fill": "both",
      "selector": "#ampdate, #sample",
      "keyframes": {"visibility": "hidden"}
    }
  </script>
</amp-animation>
<amp-date-countdown
  id="ampdate"
  end-date="2018-07-17T06:19:40+08:00"
  on="timeout: hide-timeout-event.start"
  height="400"
  width="400"
  when-ended="stop"
  locale="en"
>
  <template type="amp-mustache">
    <h1>Countdown Clock</h1>
    <div>
{{dd}} : {{hh)) : {{mm}} : {{ss}}    </div>
  </template>
</amp-date-countdown>

呈现为

验证

请参阅 AMP 验证器规范中的 amp-date-countdown 规则

需要更多帮助?

您已经阅读了本文档很多次,但它并没有真正涵盖您的所有问题?也许其他人也有同样的感受:在 Stack Overflow 上联系他们。

转到 Stack Overflow
发现错误或缺少功能?

AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的长期参与者,但我们也欢迎您针对您特别关注的问题进行一次性贡献。

转到 GitHub