首页 > 解决方案 > 如何在 amp-carousel 中添加绑定字幕?

问题描述

我需要一个轮播标题新闻。(滑块中的一些图片和文字)。

这是一个官方的例子:

<amp-carousel controls
  type="slides"
  width="400"
  height="300"
  [slide]="selectedSlide"
  on="slideChange:AMP.setState({ selectedSlide: event.index })">
  <amp-img src="/static/samples/img/image1.jpg"
    layout="fill"></amp-img>
  <amp-img src="/static/samples/img/image2.jpg"
    layout="fill"></amp-img>
  <amp-img src="/static/samples/img/image3.jpg"
    layout="fill"></amp-img>
</amp-carousel>

<h1>Selected slide: <span [text]="+selectedSlide + 1">1</span>/3</h1>

如何设置每个图像的[text]标题selectedSlide

标签: htmlcssamp-html

解决方案


我在 AMP 操场上尝试了这段代码,我可以看到选定的幻灯片文本在渲染时变化良好 - 选定的幻灯片:第一张幻灯片上的 1/3 - 选定的幻灯片:第二张幻灯片上的 2/3 - 选定的幻灯片:3/3

我不确定,但我测试了这里的代码,检查你是否错过了头部的 amp-bind 脚本?

<!doctype html>
<html ⚡&gt;
<head>
<meta charset="utf-8">
  <title>amp-carousel</title>
  <script async src="https://cdn.ampproject.org/v0.js"></script>

  <script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
  <script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
  <link rel="canonical" href="https://amp.dev/documentation/examples/components/amp-carousel/index.html">
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
  <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>


 <amp-carousel controls
  type="slides"
  width="400"
  height="300"
  [slide]="selectedSlide"
  on="slideChange:AMP.setState({ selectedSlide: event.index })">
  <amp-img src="/static/samples/img/image1.jpg"
    layout="fill"></amp-img>
  <amp-img src="/static/samples/img/image2.jpg"
    layout="fill"></amp-img>
  <amp-img src="/static/samples/img/image3.jpg"
    layout="fill"></amp-img>
</amp-carousel>

<h1>Selected slide: <span [text]="+selectedSlide + 1">1</span>/3</h1>


</body></html>

推荐阅读